android 图片旋转

 

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);
}
}
复制代码

posted @   jy02432443  阅读(21490)  评论(5编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示