How to rotate a wheel (image png) in android

孤街醉人 提交于 2020-01-03 03:41:05

问题


I want to rotate a png image on linear layout. My image is semi circle in shape and have different colors on it.

Any ideas?


回答1:


Using Matrix you can do it,

Something like,

img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

img.setImageDrawable(bmd);

OR Use Animation

RotateAnimation rAnim = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rAnim.setDuration(1000);
imageview.startAnimation(rAnim);

EDIT: Look at this question also How to make a smooth image rotation in Android?

Animating and rotating an image in a Surface View




回答2:


might be helpful for u to rotating image.......

 RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);
 rotate.setDuration(500);
 imageview.startAnimation(rotate);

first two parameter for start and end of degree.




回答3:


This is hat i have done the timer calls the pondraw mthod after every 100micro seconds h,w are height and width of the image

                    new CountDownTimer(2000, 100) {

        public void onTick(long millisUntilFinished) {
            degrees=degrees +(rm.nextFloat()*100);
            postInvalidate();
        }

        public void onFinish() {

        }
    }.start();

put this code in ondraw method

Matrix m = new Matrix();
m.setRotate(degrees,w/2,h/2);
    bmp=BitmapFactory.decodeResource(context.getResources(),R.drawable.wfortune_wheel);
canvas.drawBitmap(bmp, m, pb);

}



来源:https://stackoverflow.com/questions/8543809/how-to-rotate-a-wheel-image-png-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!