【问题标题】:in android canvas why the matrix is too slow and sluggish在 android canvas 中为什么矩阵太慢而且太迟钝
【发布时间】:2013-04-18 14:16:49
【问题描述】:

我想在 android 应用程序中围绕中心轴旋转图像,下面是代码..

package com.android.maddy.canvs;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import android.os.Handler;
import android.provider.OpenableColumns;
import android.view.MotionEvent;
import android.view.View;

public class drawview extends View {
    private Drawable d[]=new Drawable[2];

    Boolean done=false;
    Handler h;
    float th=0;
    public drawview(Context context) {
        super(context);
        d[0]=context.getResources().getDrawable(R.drawable.appdatabk);
        d[1]=context.getResources().getDrawable(R.drawable.appdata);
        h = new Handler();
    }

    Runnable r =new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            th++;
            invalidate();
        }
    };

    public void onDraw(Canvas c){
        super.onDraw(c);
        Paint p =new Paint();
        Rect imageBounds = c.getClipBounds();  // for the background
        d[0].setBounds(imageBounds);

        d[0].draw(c);

        Matrix m = new Matrix();
        m.setTranslate(getWidth()/2, getHeight()/2);        
        m.preRotate(th,43,160); //my image is 86x320

        Bitmap b =     BitmapFactory.decodeResource(getResources(),R.drawable.appdata);//image of the bottle i want to rotate
        c.drawBitmap(b,m, p);
        p.setColor(Color.BLUE);
        h.postDelayed(r,1);


    }



    }

这里发生的是矩阵计算减慢了旋转速度,因此它给出了丑陋的缓慢输出。 如果您有另一种方法可以在不使用矩阵的情况下围绕中心枢轴旋转图像,或者有其他方法,那么 请帮帮我。。 我还删除了矩阵并使用 x+r*(Math.cos(th)) 和 y+r*(Math.sin(th)) 检查了 drawBitmap() 函数中的旋转,它们工作正常,但图像不会旋转枢.. 请帮忙.. 谢谢

【问题讨论】:

    标签: android animation matrix android-canvas android-animation


    【解决方案1】:

    Matrix 相当快。让你慢下来的是你在 onDraw 中创建类和加载位图。在类中创建 Paint 和 Matrix 之一,并且还加载一次位图

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。我认为会发生什么是画布将矩阵值存储到一个数组中,并且您旋转画布的次数越多,它就会填满。

      当您使用Matrix.postRotate(degrees, px, py)Canvas.concat(Matrix) 时会发生同样的情况

      【讨论】:

        猜你喜欢
        • 2015-05-06
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        • 2012-07-15
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多