2013-03-23 60 views
1

旋轉圖像我試圖做一個假的ListView水平。爲此,我想旋轉適配器中每個單元格的內容。它幾乎工程:在列表視圖

Turn graphics

class ImageAdapter(ctx: Context, viewResourceId: Int, imageResourceId: Int, strings: Array[String], icons: TypedArray, colorSelected: Int, colorNotSelected: Int, colorBorder: Int) extends ArrayAdapter[String](ctx, viewResourceId, strings) { 
    // Useful variables 
    private val mInflater: LayoutInflater = ctx.getSystemService(
       Context.LAYOUT_INFLATER_SERVICE).asInstanceOf[LayoutInflater] 
    private val mStrings: Array[String] = strings 
    private val mIcons: TypedArray = icons 
    private val mViewResourceId: Int = viewResourceId 

    private var mSelected : Int = 0 

    def setSelected(s: Int) = mSelected = s 

    // Inherited from ArrayAdapter 
    override def getCount():Int = mStrings.length 
    override def getItem(position: Int): String = mStrings(position) 
    override def getItemId(position: Int): Long = position 

    // The rotation machinery 
    var rotation = 90 
    private var matrix = new Matrix() 
    val rect1 = new RectF(0, 0, 0, 0) 
    val rect2 = new RectF(0, 0, 0, 0) 
    val cst = Matrix.ScaleToFit.CENTER 

    // Getting the view. 
    override def getView(position: Int, convertView: View, parent: ViewGroup): View = { 
     var result = if(convertView == null) mInflater.inflate(mViewResourceId, null) else convertView 

     var iv: ImageView = result.findViewById(imageResourceId).asInstanceOf[ImageView] 
     val drawable = mIcons.getDrawable(position) 
     iv.setScaleType(ScaleType.MATRIX) 
     matrix.reset() 
     val height = drawable.getIntrinsicHeight() 
     val width = drawable.getIntrinsicWidth() 
     rotation match { 
      case 0 => 
      rect1.set(0, 0, width, height) 
      rect2.set(0, 0, iv.getWidth(), iv.getHeight()) 
      matrix.setRectToRect(rect1, rect2, cst) 
      case 90 => 
      rect1.set(0, 0, width, height) 
      rect2.set(-iv.getHeight()/2, -iv.getWidth()/2, iv.getHeight()/2, iv.getWidth()/2) 
      matrix.setRectToRect(rect1, rect2, cst) 
      matrix.postRotate(rotation, 0, 0) 
      matrix.postTranslate(iv.getWidth()/2, iv.getHeight()/2) 
      ... 
      case _ => 
     } 
     iv.setImageMatrix(matrix) 
     iv.setImageDrawable(drawable) 
     iv.postInvalidate() 

     if(position == mSelected) { 
      result.setBackgroundColor(colorSelected) 
     } else { 
      result.setBackgroundColor(colorNotSelected) 
     } 
     result.setTag(mStrings(position)) 
     result 
    } 
} 

唯一的問題是,它不顯示的第一張照片。相反,它會顯示: Listview empty

要顯示第一個列表,我需要單擊每個列表中的一個元素。的onclick方法如下:

listGraphismesView.setOnItemClickListener(new OnItemClickListener { 
    override def onItemClick(parent: AdapterView[_], view: View, position: Int, notUsedId: Long) = { 
     var graphismeName = view.getTag().asInstanceOf[String] 
     mAdapter.setSelected(position) 
     mAdapter.notifyDataSetChanged() 
     showGChallenge(position, graphismeName) 
    } 
    }) 

我試圖然後把mAdapter.notifyDataSetChanged()在片段的onResume功能,但它不更新從一開始的名單。我仍然需要點擊所有圖片才能顯示。 任何想法,爲什麼?

+0

你可以嘗試在旋轉圖像之前設置視圖的背景色嗎? – 2013-03-24 00:29:05

+0

很好的建議,但它沒有任何效果。 – 2013-03-25 21:21:29

回答

0

我發現iv.getWidth()和iv.getHeight()均返回零,因爲getview將由onActivityCreated函數被調用。將notifyDataSetChanged()放在onResume函數中還爲時尚早。所以繪製將與大小爲0繪製,當我可以點擊按鈕重新加載。

因此,要改變這種狀況,我寫了下面的代碼,調用帶有監聽調整大小功能。

var iv: ImageView = result.findViewById(imageResourceId).asInstanceOf[ImageView] 
val drawable = mIcons.getDrawable(position) 
iv.setImageDrawable(drawable) 
iv.setScaleType(ScaleType.MATRIX) 
val vto = iv.getViewTreeObserver() 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    private var matrix = new Matrix() 
    val rect1 = new RectF(0, 0, 0, 0) 
    val rect2 = new RectF(0, 0, 0, 0) 
    private var height = drawable.getIntrinsicHeight() 
    private var width = drawable.getIntrinsicWidth() 
    override def onGlobalLayout() { 
    val iv_width = iv.getWidth 
    val iv_height = iv.getHeight 
    matrix.reset() 
    rotation match { 
     case 0 => 
     rect1.set(0, 0, width, height) 
     rect2.set(0, 0, iv_width, iv_height) 
     matrix.setRectToRect(rect1, rect2, cst) 
     case 90 => 
     rect1.set(0, 0, width, height) 
     rect2.set(-iv_height/2, -iv_width/2, iv_height/2, iv_width/2) 
     matrix.setRectToRect(rect1, rect2, cst) 
     matrix.postRotate(rotation, 0, 0) 
     matrix.postTranslate(iv_width/2, iv_height/2) 
     ... 
     case _ => 
    } 
    iv.setImageMatrix(matrix) 
    } 
})