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; /** * *class name:中间箭头
*class instruction:
* @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); } }