欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android使用RotateImageView 旋转ImageView

时间:2021-08-31 10:01:06|栏目:Android代码|点击:

废话不多说了,直接给大家代码。

具体代码如下所示:

package com.droidhen.game.layout;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.widget.ImageView;
public class RotateImageView extends ImageView {
/**旋转角度**/
private float _degree;
/**旋转中心**/
private float _x;
private float _y;
/** 背景和前景资源 */
private Bitmap _bgRes, _fgRes;
/** 数字图片资源 */
private Bitmap _nums;
private Bitmap _buffer;
private Canvas _canvas;
private int start = ;
private int unitx = ;
public RotateImageView(Context context) {
super(context);
_degree = f;
_x = ;
_y = ;
_nums = BitmapFactory.decodeResource(getResources(), R.drawable.level_num);
}
public void setResIds(int bgResId, int fgResId) {
_bgRes = BitmapFactory.decodeResource(getResources(), bgResId);
_fgRes = BitmapFactory.decodeResource(getResources(), fgResId);
}
public void setLevel(int level) {
String _level = String.valueOf(level);
_buffer = Bitmap.createBitmap( _level.length()*(_nums.getWidth()/), _nums.getHeight(), Config.ARGB_);
unitx = _nums.getWidth()/;
_canvas = new Canvas(_buffer);
for(int i = ; i <= _level.length()-; i++){
Rect src = new Rect((_level.charAt(i) - '')*unitx, , (_level.charAt(i) - '' + )*unitx, _nums.getHeight());
Rect dst = new Rect( i*unitx, , (i + )*unitx, _nums.getHeight());
_canvas.drawBitmap(_nums, src, dst, null);
}
}
public void setLevel(int level, int Resid) {
_nums = BitmapFactory.decodeResource(getResources(), Resid);
String _level = String.valueOf(level);
_buffer = Bitmap.createBitmap( _level.length()*(_nums.getWidth()/), _nums.getHeight(), Config.ARGB_);
unitx = _nums.getWidth()/;
_canvas = new Canvas(_buffer);
for(int i = ; i <= _level.length()-; i++){
Rect src = new Rect((_level.charAt(i) - '')*unitx, , (_level.charAt(i) - '' + )*unitx, _nums.getHeight());
Rect dst = new Rect( i*unitx, , (i + )*unitx, _nums.getHeight());
_canvas.drawBitmap(_nums, src, dst, null);
}
}
public void setDegree(float degree) {
_degree = degree;
}
public void setDegree(float degree, float x, float y) {
_degree = degree;
_x = x;
_y = y;
}
public float getDegree() {
return _degree;
}
public float getRotateX() {
return _x;
}
public float getRotateY() {
return _y;
}
@Override
public void draw(Canvas canvas) {
canvas.save();
canvas.setDrawFilter(new PaintFlagsDrawFilter(, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG)); 
canvas.rotate(_degree, _x, _y);
canvas.drawBitmap(_bgRes, , , null);
canvas.drawBitmap(_fgRes, , , null);
canvas.drawBitmap(_buffer, (_bgRes.getWidth() - _buffer.getWidth())/f, , null);
canvas.restore();
}
}

以上代码是关于Android RotateImageView 可旋转ImageView 的全部内容,希望对大家有所帮助。

上一篇:解析Android开发优化之:对界面UI的优化详解(二)

栏    目:Android代码

下一篇:实现qq中按返回键返回桌面不退出程序的实例

本文标题:Android使用RotateImageView 旋转ImageView

本文地址:http://www.codeinn.net/misctech/171491.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有