以不同的速度旋转两个图像

时间:2011-10-21 07:34:34

标签: android

下面的代码用于以相同的速度旋转两个图像,我想以不同的速度旋转两个图像,我想在我的应用程序中使用两个画布,如何在此应用程序中添加两个画布使(canvas.rotate(方向) ,width / 2,height / 2);)我可以以不同的速度旋转两个图像。感谢

public class Rotate extends ImageView {
    Paint paint;
    int direction = 0;
    int degree = 0;
    int rot;
    private float x;
    private float y;
    private float centerX;
    private float centerY;
    private float newX;
    private float newY;
    boolean rotation = false;
    private Bitmap mBitmap;
    private Bitmap mWood;
    private Bitmap icon, mBitmap1, rollerDesk;
    private Canvas ball2Rotate;
    private Bitmap ball;
    Bitmap rollerBitmap;



    public Rotate(Context context) {
        super(context);

        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(2);
        paint.setStyle(Style.STROKE);


        Options opts = new Options();
        opts.inDither = true;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;

        mWood = BitmapFactory.decodeResource(getResources(),
                R.drawable.newwood, opts);

        Bitmap ball = BitmapFactory.decodeResource(getResources(),
                R.drawable.roller);
        mBitmap = Bitmap.createScaledBitmap(ball, 50, 50, true);

        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.ball, opts);
        mBitmap1 = Bitmap.createScaledBitmap(icon, 50, 50, true);

        rollerBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.imagesnic, opts);
        rollerDesk = Bitmap.createScaledBitmap(rollerBitmap, 400, 400, true);

        this.setImageResource(R.drawable.casinonic);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int height = this.getHeight();
        int width = this.getWidth();
        ball2Rotate = canvas;
        final Bitmap bitmap = mBitmap;
        final Bitmap bitmap1 = mBitmap1;

        centerX = width / 2;
        centerY = height / 2;
        canvas.drawBitmap(mWood, 0, 0, null);
        canvas.drawBitmap(bitmap, width / 2, height / 2, paint);

        ball = bitmap1;
//      rotateball(canvas, bitmap1);

        canvas.rotate(direction, width / 2, height / 2);
//      canvas.rotate(direction, width / 2, height / 2);
        super.onDraw(canvas);
        this.invalidate();

        canvas.drawBitmap(rollerDesk, width / 12, height / 4, paint);
        canvas.drawBitmap(bitmap1, 220, 160, paint);

        if (rotation) {
            direction += rot;
            rot = rot + 1;

            Log.d("Direction values in ONDRAW" + direction, "Rotation values "
                    + rot);
            if (rot == 100) {
                rotation = false;

            }
        }
    }

    private void rotateball(Canvas canvas, Bitmap bitmap1) {

        canvas.drawBitmap(bitmap1, newX + 220, newY + 400, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            x = event.getX();
            y = event.getY();
            System.out.println("x and y coordinates" + x + y);
            newX = centerX - x;
            newY = centerY - y;
            rotation = false;
            updateRotation(newX, newY);

        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {

            x = event.getX();
            y = event.getY();

            newX = centerX - x;
            newY = centerY - y;
            rotation = false;
            updateRotation(newX, newY);

        } else if (event.getAction() == MotionEvent.ACTION_UP) {

            x = event.getX();
            y = event.getY();

            newX = centerX - x;
            newY = centerY - y;
            rot = 0;
            rotation = true;

            updateRotation(newX, newY);

        }
        return true;
    }

    private void setDirection1(int degree2) {


        for (int j = 0; j < 10; j++) {

            this.direction = degree2;

            degree2 += 5;

        }
        this.invalidate();

    }

    private void updateRotation(float newX2, float newY2) {

        degree = (int) Math.toDegrees(Math.atan2(newY, newX));

        setDirection1(degree);

    }

}

2 个答案:

答案 0 :(得分:0)

为什么不使用相同的画布?

canvas.save();
canvas .rotate(rotationAngleForBitmap1);
canvas .drawBitmap(bitmap, matrix, paint);
canvas .restore();
canvas .rotate(rotationAngleForBitmap2);
canvas .drawBitmap(bitmap2, matrix, paint)
canvas .restore();

答案 1 :(得分:0)

下面是android中使用相同画布和旋转功能的模拟时钟代码。希望这会对你有所帮助。如果您需要完整的代码,请告诉我。

 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        boolean changed = mChanged;
        if (changed) {
            mChanged = false;
        }
        boolean seconds = mSeconds;
        if (seconds ) {
            mSeconds = false;
        }
        int availableWidth = 200;
        int availableHeight = 200;

        int x = availableWidth / 2;
        int y = availableHeight / 2;

        final Drawable dial = mDial;
        int w = dial.getIntrinsicWidth();
        int h = dial.getIntrinsicHeight();

        boolean scaled = false;

        if (availableWidth < w || availableHeight < h) {
            scaled = true;
            float scale = Math.min((float) availableWidth / (float) w,
                                   (float) availableHeight / (float) h);
            canvas.save();
            canvas.scale(scale, scale, x, y);
        }

        if (changed) {
            dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
        }
        dial.draw(canvas);

        canvas.save();
        canvas.rotate(mHour / 12.0f * 360.0f, x, y);
        final Drawable hourHand = mHourHand;
        if (changed) {
            w = hourHand.getIntrinsicWidth();
            h = hourHand.getIntrinsicHeight();
            hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
        }
        hourHand.draw(canvas);
        canvas.restore();

        canvas.save();
        canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);
        //canvas.rotate(mSecond, x, y);
        final Drawable minuteHand = mMinuteHand;
        if (changed) {
            w = minuteHand.getIntrinsicWidth();
            h = minuteHand.getIntrinsicHeight();
            minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
        }
        minuteHand.draw(canvas);
        canvas.restore();
       canvas.save();
        canvas.rotate(mSecond, x, y);

        //minuteHand = mMinuteHand;
        if (seconds) {
            w = mSecondHand.getIntrinsicWidth();
            h = mSecondHand.getIntrinsicHeight();
            mSecondHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
        }
        mSecondHand.draw(canvas);
        canvas.restore();
        if (scaled) {
            canvas.restore();
        }
    }