如何在android中顺时针和逆时针旋转图像(指尖旋转)?

如何在android中顺时针和逆时针旋转图像(指尖旋转)?,android,android-image,image-rotation,Android,Android Image,Image Rotation,当我在屏幕上旋转我的指尖时,我必须按时钟方向和逆时针方向旋转叠加图像,不想以特定角度旋转。如果任何机构有解决方案,请帮助我。请尝试下面的链接,并在尝试之前检查链接:- public void Rotate(View v) { ImageView img = (ImageView)findViewById(R.id.imageView01); Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.

当我在屏幕上旋转我的指尖时,我必须按时钟方向和逆时针方向旋转叠加图像,不想以特定角度旋转。如果任何机构有解决方案,请帮助我。

请尝试下面的链接,并在尝试之前检查链接:-

 public void Rotate(View v)
{

    ImageView img = (ImageView)findViewById(R.id.imageView01);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.bharath);
// Getting width & height of the given image.
        int w = bmp.getWidth();
        int h = bmp.getHeight();
// Setting pre rotate to 90
        Matrix mtx = new Matrix();
        mtx.preRotate(90);
// Rotating Bitmap
        Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
        BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
        img.setImageBitmap(rotatedBMP);
    }
试试这个

AndroidBitmap.java

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Spinner;

public class AndroidBitmap extends Activity {

    //private final String imageInSD = "/sdcard/er.PNG";

    ImageView myImageView;
    Spinner spinnerScale;
    SeekBar seekbarRotate;

    private static final String[] strScale 
        = {"0.2x", "0.5x", "1.0x", "2.0x", "5.0x"}; 
    private static final Float[] floatScale 
        = {0.2F, 0.5F, 1F, 2F, 5F};
    private final int defaultSpinnerScaleSelection = 2;

    private ArrayAdapter<String> adapterScale;

    private float curScale = 1F;
    private float curRotate = 0F;

    Bitmap bitmap;
    int bmpWidth, bmpHeight;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myImageView = (ImageView)findViewById(R.id.imageview);

        spinnerScale = (Spinner)findViewById(R.id.scale);
        seekbarRotate = (SeekBar)findViewById(R.id.rotate);

        adapterScale = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, strScale);
        adapterScale.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerScale.setAdapter(adapterScale);
        spinnerScale.setSelection(defaultSpinnerScaleSelection);
        curScale = floatScale[defaultSpinnerScaleSelection];

        BitmapDrawable img = (BitmapDrawable) getResources()
                .getDrawable(R.drawable.ic_launcher);


        bitmap = img.getBitmap();//BitmapFactory.decodeFile(imageInSD);
        bmpWidth = bitmap.getWidth();
        bmpHeight = bitmap.getHeight();

        drawMatrix();

        spinnerScale.setOnItemSelectedListener(spinnerScaleOnItemSelectedListener);
        seekbarRotate.setOnSeekBarChangeListener(seekbarRotateSeekBarChangeListener);

    }

    private void drawMatrix(){

        Matrix matrix = new Matrix();
        matrix.postScale(curScale, curScale);
        matrix.postRotate(curRotate);

        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
        myImageView.setImageBitmap(resizedBitmap);

    }

    private SeekBar.OnSeekBarChangeListener seekbarRotateSeekBarChangeListener
        = new SeekBar.OnSeekBarChangeListener(){

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                curRotate = (float)progress;
                drawMatrix();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }};

    private Spinner.OnItemSelectedListener spinnerScaleOnItemSelectedListener
        = new Spinner.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                curScale = floatScale[arg2];
                drawMatrix();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                spinnerScale.setSelection(defaultSpinnerScaleSelection);
                curScale = floatScale[defaultSpinnerScaleSelection];
            }};
}
导入android.app.Activity;
导入android.graphics.Bitmap;
导入android.graphics.Matrix;
导入android.graphics.drawable.BitmapDrawable;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.SeekBar;
导入android.widget.Spinner;
公共类AndroidBitmap扩展活动{
//私有最终字符串imageInSD=“/sdcard/er.PNG”;
ImageView我的ImageView;
喷丝器;
SeekBar seekbarRotate;
私有静态最终字符串[]strScale
={“0.2x”、“0.5x”、“1.0x”、“2.0x”、“5.0x”};
私有静态最终浮点[]浮点刻度
={0.2F,0.5F,1F,2F,5F};
私有最终int defaultSpinnerScaleSelection=2;
专用阵列适配器刻度;
专用浮动光标=1F;
专用浮点数=0F;
位图;
int bmpWidth,bmpHeight;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myImageView=(ImageView)findViewById(R.id.ImageView);
喷丝头刻度=(喷丝头)findViewById(R.id.scale);
seekbarRotate=(SeekBar)findViewById(R.id.rotate);
adapterScale=新阵列适配器(此,
android.R.layout.simple_spinner_项目,strScale);
adapterScale.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头刻度。设置适配器(适配器刻度);
喷丝头刻度.设置选择(默认喷丝头刻度选择);
游标刻度=浮动刻度[defaultSpinnerScaleSelection];
BitmapDrawable img=(BitmapDrawable)getResources()
.getDrawable(R.drawable.ic_发射器);
bitmap=img.getBitmap();//BitmapFactory.decodeFile(imageInSD);
bmpWidth=bitmap.getWidth();
bmpHeight=bitmap.getHeight();
drawMatrix();
spinnerScale.setOnItemSelectedListener(spinnerScaleOnItemSelectedListener);
seekbarRotate.setonseekbarchaneglistener(seekbarrotatesekbarchaneglistener);
}
私有矩阵(){
矩阵=新矩阵();
矩阵。后标度(粗标度,粗标度);
旋转后基质(咖喱酸盐);
Bitmap resizedBitmap=Bitmap.createBitmap(位图,0,0,bmpWidth,bmpHeight,matrix,true);
myImageView.setImageBitmap(resizedBitmap);
}
private SeekBar.onseekbarchaneglistener seekbarrotate seekbarchaneglistener
=新SeekBar.onseekbarchaneglistener(){
@凌驾
更改了公共进程无效(SeekBar SeekBar,int progress,
布尔值(用户){
//TODO自动生成的方法存根
curRotate=(浮动)进度;
drawMatrix();
}
@凌驾
开始跟踪触摸时的公共无效(SeekBar SeekBar){
//TODO自动生成的方法存根
}
@凌驾
TopTrackingTouch(SeekBar SeekBar)上的公共无效{
//TODO自动生成的方法存根
}};
private Spinner.OnItemSelectedListener spinnerScaleOnItemSelectedListener
=新微调器。OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
//TODO自动生成的方法存根
游标刻度=浮动刻度[arg2];
drawMatrix();
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
喷丝头刻度.设置选择(默认喷丝头刻度选择);
游标刻度=浮动刻度[defaultSpinnerScaleSelection];
}};
}
布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello, Android image rotate"
    />
<Spinner
    android:id="@+id/scale"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />
<SeekBar
    android:id="@+id/rotate"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_margin="5px"
    android:max="360"
    android:progress="0"
    />
<ImageView  
    android:id="@+id/imageview"
    android:layout_gravity="center"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scaleType="center"
    />
</LinearLayout>


希望这有帮助

检查这个答案也许对你有帮助:

什么是“图像”?什么是“覆盖”?你是指动画,还是旋转源图像本身?你可以多解释一点,或者添加你已经拥有的代码段将有更多帮助。你可以使用自定义图像视图并在draw@Commonware--overlay上旋转画布-我有一个标题所在的布局。所以当我旋转图像时,旋转后的图像应该在标题后面旋转,而不是在标题上方。用手指触摸,嗯!你以前为什么不说。在这里,你可以得到手指被触碰的位置坐标,捕捉运动,然后使用上面的逻辑,我希望这能解决问题。非常非常感谢老兄。。。它工作得很好。但我需要在多个手指(主要是两个手指)的触摸下旋转图像,只有一个手指除外。用一根手指可以很好地工作,但当我用两根手指使用它时,它先前的位置会随着眨眼而改变。所以你能帮我找到解决这个问题的办法吗。提前谢谢。