如何在不改变其大小旋转图像?不改变、图像、大小、如何在

2023-09-07 11:23:29 作者:?在沙滩中与你徘徊。

我正在开发Android应用程序,我必须围绕另一个圆形图像旋转的圆形图像,但是当我跑这我圆形图像的大小就会自动持续变化。所以,请帮助我如何解决这个问题。这里是我的code

 公共类职位搜索扩展活动实现OnTouchListener {    私人ImageView的拨号器;    私人浮动Y = 0;    公共布尔onTouch(视图V,MotionEvent事件){        双R = Math.atan2(event.getX() -  dialer.getWidth()/ 2,dialer.getHeight()/ 2-event.getY());        INT旋转=(int)的Math.toDegrees(R);        开关(event.getAction()){            案例MotionEvent.ACTION_DOWN:                打破;            案例MotionEvent.ACTION_MOVE:                //x=event.getX();                Y = event.getY();                updateRotation(旋转);                打破;            案例MotionEvent.ACTION_UP:                打破;        }//开关     返回true;    } // onTouch    私人无效updateRotation(双腐){        浮newRot =新的浮动(腐);        位图位图= BitmapFactory.de codeResource(getResources(),R.drawable.round_button_big);        字模=新的Matrix();        matrix.postRotate(newRot); //,bitmap.getWidth()/ 2,bitmap.getHeight()/ 2);        位图reDrawnBitmap = Bitmap.createBitmap(位图,0,0,bitmap.getWidth(),bitmap.getHeight(),矩阵,真);        dialer.setImageBitmap(reDrawnBitmap);        如果(newRot> =  -  5和;&放大器; newRot&下; = 5)            Toast.makeText(这一点,12 O \\'时钟,Toast.LENGTH_SHORT).show();        如果(newRot> = 85安培;&安培; newRot< = 95)            Toast.makeText(这一点,3 O \\'时钟,Toast.LENGTH_SHORT).show();        如果(newRot> = 175 || newRot< =  -  175)            Toast.makeText(这一点,6 O \\'时钟,Toast.LENGTH_SHORT).show();        如果(newRot> =  -  95安培;&安培; newRot< =  -  85)            Toast.makeText(这一点,九O \\'时钟,Toast.LENGTH_SHORT).show();    }    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);        拨号=(ImageView的)findViewById(R.id.big_button);        dialer.setOnTouchListener(本);    } //的onCreate} 

解决方案

试试这个code的旋转图像 -

 公共类bitmaptest延伸活动{@覆盖公共无效的onCreate(捆绑冰柱){    super.onCreate(冰柱);    的LinearLayout linLayout =新的LinearLayout(本);    //加载origial位图(500×500像素)    位图bitmapOrg = BitmapFactory.de codeResource(getResources()           R.drawable.android);    INT宽度= bitmapOrg.width();    INT高度= bitmapOrg.height();    INT newWidth = 200;    INT newHeight = 200;    //计算比例 - 在这种情况下= 0.4f    浮动scaleWidth =((浮点)newWidth)/宽度;    浮动scaleHeight =((浮点)newHeight)/身高;    //用于操作createa矩阵    字模=新的Matrix();    //调整位图    matrix.postScale(scaleWidth,scaleHeight);    //旋转位图    matrix.postRotate(45);    //重新创建新的位图    位图resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,                      宽度,高度,矩阵,真);    //使从位图可绘制允许设置位图    //为ImageView的,ImageButton的或什么都    BitmapDrawable BMD =新BitmapDrawable(resizedBitmap);    ImageView的ImageView的=新ImageView的(本);    //设置绘制对象上的ImageView    imageView.setImageDrawable(BMD);    //中心图像    imageView.setScaleType(ScaleType.CENTER);    //添加的ImageView的布局    linLayout.addView(ImageView的,            新LinearLayout.LayoutParams(                  LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT            )    );    //设置的LinearLayout作为内容查看    的setContentView(linLayout);}} 

和,也看到这个教程

如何在不改变照片尺寸的情况下改变其大小

I am developing an android app where I have to rotate a circular image around another circular image, but when I am running this the size of my circular image gets automatically changing continuously. So please help me how to solve this. Here is my code

public class JobSearch extends Activity implements OnTouchListener {
    private ImageView dialer;
    private float y=0;
    public boolean onTouch(View v, MotionEvent event) {
        double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
        int rotation=(int)Math.toDegrees(r);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                //x=event.getX();
                y=event.getY();
                updateRotation(rotation);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }//switch       
     return true;    
    }//onTouch
    private void updateRotation(double rot){
        float newRot=new Float(rot);
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.round_button_big);
        Matrix matrix=new Matrix();
        matrix.postRotate(newRot);//,bitmap.getWidth()/2,bitmap.getHeight()/2);
        Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        dialer.setImageBitmap(reDrawnBitmap);
        if(newRot>=-5 && newRot<=5)
            Toast.makeText(this,"12 O\'Clock",Toast.LENGTH_SHORT).show();
        if(newRot>=85 && newRot<=95)
            Toast.makeText(this,"3 O\'Clock",Toast.LENGTH_SHORT).show();
        if(newRot>=175 || newRot<=-175)
            Toast.makeText(this,"6 O\'Clock",Toast.LENGTH_SHORT).show();
        if(newRot>=-95 && newRot<=-85)
            Toast.makeText(this,"9 O\'Clock",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialer = (ImageView) findViewById(R.id.big_button);
        dialer.setOnTouchListener(this);
    }//onCreate
}

解决方案

Try this code for Rotating Image -

public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    LinearLayout linLayout = new LinearLayout(this);

    // load the origial BitMap (500 x 500 px)
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
           R.drawable.android);

    int width = bitmapOrg.width();
    int height = bitmapOrg.height();
    int newWidth = 200;
    int newHeight = 200;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                      width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    ImageView imageView = new ImageView(this);

    // set the Drawable on the ImageView
    imageView.setImageDrawable(bmd);

    // center the Image
    imageView.setScaleType(ScaleType.CENTER);

    // add ImageView to the Layout
    linLayout.addView(imageView,
            new LinearLayout.LayoutParams(
                  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
            )
    );

    // set LinearLayout as ContentView
    setContentView(linLayout);
}
}

And, also see this Tutorial