Matrix 旋转图片 Matrix xMatrix = new Matrix(); xMatrix.set(mBaseMatrix); xMatrix.setRotate(-90); setImageMatrix(xMatrix); float v[] = new float[9]; xMatrix.getValues(v); mBaseMatrix.setValues(v); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 图片旋转public class BeadplateMiddle extends View { private Bitmap mBitmap = null;//声明Bitmap对象 private int x = 300; private int y = 100; //private float angleA = 0.0f;//声明轴心A转动 private float angleB = 0.0f;//声明轴心B转动角度 private Matrix mMatrix = new Matrix();//构建矩阵Matrix对象 public BeadplateMiddle(Context context) { super(context); //装载资源 mBitmap =((BitmapDrawable) getResources().getDrawable(R.drawable.bedplate)).getBitmap(); //开启线程 new Thread(new DrawThread()).start(); } //系统IOC开始绘制 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setAntiAlias(true); canvas.drawCircle(x, y, 12, mPaint); mMatrix = getMyMatrix(mMatrix,angleB, x, y); canvas.drawBitmap(mBitmap, mMatrix, null); } /** * 动态构建旋转矩阵Matrix对象 * @param matrix 需要计算的矩阵 * @param canvas 画布 * @param degrees 图片旋转的角度,正值为顺时针,负值为逆时针 * @param pivotX 轴心的X坐标 * @param pivotY 轴心的Y坐标 */ private Matrix getMyMatrix(Matrix matrix ,float degrees,int pivotX , int pivotY ){ //重置Matrix matrix.reset(); float cosValue = (float) Math.cos(Math.PI/(180/degrees)); float sinValue = (float) Math.sin(Math.PI/(180/degrees)); //设置旋转矩阵值 matrix.setValues( new float[]{ cosValue, -sinValue, pivotX, sinValue, cosValue, pivotY, 0, 0, 1}); return matrix; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){ x --; }else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){ x ++; }else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ y --; }else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){ y ++; }else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){ angleB ++; } return true; } private class DrawThread implements Runnable{ @Override public void run() { while(!Thread.currentThread().isInterrupted()){ try { Thread.sleep(500); } catch (Exception e) { Thread.currentThread().interrupt(); } //使用PostInvalidate可以直接在线程中更新视图 postInvalidate(); } } } } 因为你每次都是new一个matrix出来对原图进行操作,把上次的matrix保存下来就可以了 #4楼的意思是是像这样Matrix xMatrix = new Matrix();while(true){ xMatrix.set(mBaseMatrix); xMatrix.setRotate(-90); setImageMatrix(xMatrix); float v[] = new float[9]; xMatrix.getValues(v); mBaseMatrix.setValues(v);}让你的xMatrix成为范围更大的局部资源 关键你setImageMatrix这个方法怎么写的,看图片是不是一直是原始的Bitmap对象。 Android网络业务封装的设计问题? Android播放rtsp协议视频的问题 Android传感器API之:磁场Magnetic Field源码与示例 Android 使用Matrix缩放图片的问题! fragment的问题 android图片轮放效果 要实现一个在andriod手机上通过“打开”按钮能读取服务器的文件 怎么小球没运动 和视频里一样的代码,自己写出来却不能运行,并且没有报错,求指教 安卓新手求助,做一个安卓读取webserice的demo就行 android 调用默认铃声后怎么改起始的默认选择位置? 为什么 android 开发手机拨号器的时候另外一台手机接收不到
private Bitmap mBitmap = null;//声明Bitmap对象
private int x = 300;
private int y = 100;
//private float angleA = 0.0f;//声明轴心A转动
private float angleB = 0.0f;//声明轴心B转动角度
private Matrix mMatrix = new Matrix();//构建矩阵Matrix对象
public BeadplateMiddle(Context context) {
super(context);
//装载资源
mBitmap =((BitmapDrawable) getResources().getDrawable(R.drawable.bedplate)).getBitmap();
//开启线程
new Thread(new DrawThread()).start();
}
//系统IOC开始绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setAntiAlias(true);
canvas.drawCircle(x, y, 12, mPaint);
mMatrix = getMyMatrix(mMatrix,angleB, x, y);
canvas.drawBitmap(mBitmap, mMatrix, null);
}
/**
* 动态构建旋转矩阵Matrix对象
* @param matrix 需要计算的矩阵
* @param canvas 画布
* @param degrees 图片旋转的角度,正值为顺时针,负值为逆时针
* @param pivotX 轴心的X坐标
* @param pivotY 轴心的Y坐标
*/
private Matrix getMyMatrix(Matrix matrix ,float degrees,int pivotX , int pivotY ){
//重置Matrix
matrix.reset();
float cosValue = (float) Math.cos(Math.PI/(180/degrees));
float sinValue = (float) Math.sin(Math.PI/(180/degrees));
//设置旋转矩阵值
matrix.setValues(
new float[]{
cosValue, -sinValue, pivotX,
sinValue, cosValue, pivotY,
0, 0, 1});
return matrix;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
x --;
}else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
x ++;
}else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
y --;
}else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
y ++;
}else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
angleB ++;
}
return true;
}
private class DrawThread implements Runnable{
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try {
Thread.sleep(500);
} catch (Exception e) {
Thread.currentThread().interrupt();
}
//使用PostInvalidate可以直接在线程中更新视图
postInvalidate();
}
}
}
}
Matrix xMatrix = new Matrix();
while(true)
{
xMatrix.set(mBaseMatrix);
xMatrix.setRotate(-90);
setImageMatrix(xMatrix);
float v[] = new float[9];
xMatrix.getValues(v);
mBaseMatrix.setValues(v);
}让你的xMatrix成为范围更大的局部资源