android圖片旋轉是老話題了,但如何簡單迅速無BUG的解決它是需要想一想的。
首先回顧一下android 圖片旋轉提供的API有哪些
1.Matrix
2.Animation
說白了 Animation的核心也是Matrix。 而且Animation真是擴充性不強也不好用,用過的應該都知道。
那我們開始走Matrix路線吧。下面是遊戲中用到的箭頭選擇並動畫在原地來回位移。
核心代碼如下
package com.laahaa.view.extension;
import com.laahaa.R;
import com.laahaa.config.ExtensionLayoutConfig;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
/**
*
* <p>class name:中間箭頭</p>
* <p>class instruction:</p>
* @author Mercury Create in 2012-2-7
*/
public class CenterArrow extends ImageView{
FrameLayout.LayoutParams lp;
private Bitmap bitmap;
private Matrix matrix = new Matrix();
public CenterArrow(Context context) {
super(context);
lp=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.CENTER);
}
/**
* 線程每次間隔時間調用
* @param dir
*/
public void onTick(int dir)
{
switch (dir) {
case 0:
if(lp.bottomMargin==(ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth))
lp.bottomMargin+=ExtensionLayoutConfig.ArrowOffset;
else
lp.bottomMargin-=ExtensionLayoutConfig.ArrowOffset;
break;
case 1:
if(lp.leftMargin==(ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth))
lp.leftMargin+=ExtensionLayoutConfig.ArrowOffset;
else
lp.leftMargin-=ExtensionLayoutConfig.ArrowOffset;
break;
case 2:
if(lp.bottomMargin==-(ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth))
lp.bottomMargin-=ExtensionLayoutConfig.ArrowOffset;
else
lp.bottomMargin+=ExtensionLayoutConfig.ArrowOffset;
break;
case 3:
if(lp.leftMargin==-(ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth))
lp.leftMargin-=ExtensionLayoutConfig.ArrowOffset;
else
lp.leftMargin+=ExtensionLayoutConfig.ArrowOffset;
break;
}
this.setLayoutParams(lp);
this.setImageBitmap(bitmap);
}
public void setRotate(int dir)
{
int degrees=0;
switch (dir) {
case 0:
degrees=90;
lp.leftMargin=0;
lp.rightMargin=0;
lp.bottomMargin=ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth;
lp.topMargin=0;
break;
case 1:
degrees=0;
lp.leftMargin=ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth;
lp.rightMargin=0;
lp.bottomMargin=0;
lp.topMargin=0;
break;
case 2:
degrees=270;
lp.leftMargin=0;
lp.rightMargin=0;
lp.bottomMargin=-(ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth);
lp.topMargin=0;
break;
case 3:
degrees=180;
lp.leftMargin=-(ExtensionLayoutConfig.CenterArrowWidth+ExtensionLayoutConfig.CenterCircleWidth);
lp.rightMargin=0;
lp.bottomMargin=0;
lp.topMargin=0;
break;
}
bitmap=((BitmapDrawable) getResources().getDrawable(R.drawable.arrow)).getBitmap();
this.setLayoutParams(lp);
//設定映像的旋轉角度
matrix.setRotate(degrees);
//旋轉映像,並產生新的Bitmap對像
bitmap=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
//重新在ImageView組件中顯示旋轉後的映像
this.setImageBitmap(bitmap);
}
}