【问题标题】:How to rotate image clockwise and anti-clockwise in android(At finger-tip rotation)?如何在android中顺时针和逆时针旋转图像(指尖旋转)?
【发布时间】:2013-05-07 11:20:03
【问题描述】:

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

【问题讨论】:

  • 什么是“图像”?什么是“覆盖”?
  • 你是指动画,还是旋转源图像本身?
  • 你可以多解释一点,或者添加你已经拥有的代码段会更有帮助
  • 您可以使用自定义图像视图并在绘制时旋转画布
  • @CommonsWare--覆盖-我有一个布局,其中有一个标题。因此,当我旋转图像时,旋转后的图像应该在该标题后面旋转,而不是在该标题上方。

标签: android android-image image-rotation


【解决方案1】:

检查此答案可能对您有帮助:How can i use RotateAnimation to rotate a circle?

【讨论】:

  • 非常感谢老兄...它工作得很好。但是我需要在触摸多个手指(主要是两个)时旋转图像,除了一个。用 1 根手指可以正常工作,但是当我用 2 根手指使用它时,它之前的位置会随着眨眼而改变。所以你能帮我找到这个问题的解决方案吗?提前致谢。
【解决方案2】:

尝试以下链接并在尝试之前检查this链接:-

 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);
    }

【讨论】:

    【解决方案3】:

    试试这个

    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];
                }};
    }
    

    布局文件

    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>
    

    希望对你有帮助

    【讨论】:

    • 手指触摸 嗯!为什么你之前不说。 Check out this link。在这里,您可以获得手指触摸位置的坐标,捕捉动作,然后使用上述逻辑,我希望这可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2011-07-26
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多