在android中旋转图像时调整图像大小 [英] Resize image while rotating image in android

查看:116
本文介绍了在android中旋转图像时调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android项目,在该项目中我希望将图像连同触摸一起旋转到某个固定的枢轴点.我已经完成了所有这些工作,但是我遇到了一个问题:在尝试旋转图像时,图像位图的大小已调整.我不知道为什么会发生.如果有人这样做,请给我一个主意,以帮助克服这个问题.

I'm working with android project in which I want to rotate image along with touch to some fix pivot point. I have completed all these things but I am facing one problem: While I'm trying to rotate image the image bitmap is resized. I dont have any idea why it occurs. If somebody has then please give me an idea to help overcome this problem.

我的代码:

package com.demo.rotation;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class temp  extends Activity{

ImageView img1;
float startX;
float startX2 ;

Bitmap source;
Bitmap bitmap1 = null;

double r;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    img1 = (ImageView) findViewById(R.id.img1);
    img1.setOnTouchListener(img1TouchListener);

    bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.orsl_circle_transparent);
}

private OnTouchListener img1TouchListener = new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_MOVE:
                Log.d("MOVE", "1");
                if(source!=null)
                 r = Math.atan2(event.getX() - source.getWidth(),
                        (source.getHeight() / 2) - event.getY());

                Log.i("startX" + event.getX(), "startY" + event.getY());
                     rotate(r,bitmap1, img1);
                     img1.setScaleType(ScaleType.CENTER);

                break;

            case MotionEvent.ACTION_DOWN:

                break;

            case MotionEvent.ACTION_UP:
                break;

            default :
                break;
        }
        return true;
    }
};

private void rotate(double r , Bitmap currentBitmap  ,ImageView imageView )
{
    int rotation = (int) Math.toDegrees(r);
    Matrix matrix = new Matrix();

    matrix.setRotate(rotation, currentBitmap.getWidth()/2, currentBitmap.getHeight()/2);
    source = Bitmap.createBitmap(currentBitmap, 0, 0, currentBitmap.getWidth(), currentBitmap.getHeight(), matrix, false);
    imageView.setImageBitmap(source);
    Log.i("HIGHT OF CURRENT BITMAP", ""+source.getHeight());
}

}

推荐答案

您不旋转图像,而是旋转画布.缩放是使用setBounds()完成的.请参阅此问题以寻求帮助.

You don't rotate the image, you rotate the canvas. Scaling is done using setBounds(). See this question for a bit of help.

这篇关于在android中旋转图像时调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆