图片旋转
来源:互联网 发布:汇川plc软件 编辑:程序博客网 时间:2024/04/29 10:44
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); }}
- 图片旋转
- 图片旋转
- 旋转图片
- 图片旋转
- 图片旋转
- 旋转图片
- 旋转图片
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 图片旋转
- 旋转图片
- 图片旋转
- 图片旋转
- 图片旋转
- IT职场人生系列之二十四:程序员如何增加收入
- java 8种排序
- Postgres数据库忘记密码,三个步骤解决(windows下)
- Linux系统的休眠与唤醒
- 2012年12月第五个周末
- 图片旋转
- C++string 类常用函数的函数原型
- lambda表达式(python & C++)
- 改变 input file 样式的两种方法
- Sed命令的用法
- hibernate的继承分页bug:firstResult/maxResults specified on polymorphic query; applying in memory!
- android 各国语言对应的缩写
- WebGL Tutorial 创建WebGL环境
- Unix常见的信号以及信号定义