android旋转图像
我试图创建两个旋转的轮子,就像在滑轮中一样,所以每次连接的绳子移动时,两个滑轮都会旋转。我尝试了两种方法: 1) 在View类的onDraw()方法中使用Matrix.postRotate,该方法调用以下内容:android旋转图像,android,image,spinning,Android,Image,Spinning,我试图创建两个旋转的轮子,就像在滑轮中一样,所以每次连接的绳子移动时,两个滑轮都会旋转。我尝试了两种方法: 1) 在View类的onDraw()方法中使用Matrix.postRotate,该方法调用以下内容: private void drawSpinningWheel(Canvas canvas) { try { canvas.save(); Bitmap bitmapOrg = null; int iDrawabl
private void drawSpinningWheel(Canvas canvas)
{
try
{
canvas.save();
Bitmap bitmapOrg = null;
int iDrawable = R.drawable.spinning_button;
bitmapOrg = BitmapFactory.decodeResource(getResources(), iDrawable);
if(bitmapOrg != null)
{
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 24;
int newHeight = 24;
// 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);
matrix.postRotate((float) mDegrees++);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
canvas.drawBitmap(resizedBitmap, matrix, null);
}
canvas.restore();
}
catch(Exception e)
{
Log.e(TAG + "drawSpinningWheel", e.getMessage());
}
}
但看起来图像不仅会旋转,还会绕另一个轴旋转
2) 使用SurfaceView和一个单独的线程,在run()中调用:
private void doDraw(Canvas canvas) {
// Draw the background image. Operations on the Canvas accumulate
// so this is like clearing the screen.
canvas.drawBitmap(mBackgroundImage, 0, 0, null);
int yTop = mCanvasHeight - ((int) mY + mSpinningWheelImageHeight / 2);
int xLeft = (int) mX - mSpinningWheelImageWidth / 2;
我让纺车运转起来,但我不能再加一个纺车。我甚至试图为第二个纺车创建另一条线,但只显示了一条。有人能给我指出正确的方向吗?谢谢
3) 好的,现在我也尝试了旋转动画:
公共类GraphicsView扩展视图
{
私有静态最终字符串引号=
“再也没有人使用Java了。它是一个巨大的重量级游戏。”
}
但结果与画布相似。旋转,我能够使第一个图像旋转良好,但第二个图像将简单地围绕第一个图像旋转,尽管我注意到第二个图像实际上也在旋转。看起来第二张图片我需要一个单独的视图,我希望避免这种情况,因为我不确定如何使轮子旋转,但连接的绳子不旋转。你尝试过旋转动画吗?你尝试过旋转动画吗?查看我的问题和答案,我想这就是你(和我)的想法你在找。。。
我放了三个动画,我的标志文字和我的标志图标。然后我在文本上做了一个负旋转,在图标上做了一个正旋转。它很简单但很酷。我的问题的答案有代码清单。查看我的问题及其答案,我想这就是你(和我)正在寻找的。。。
我放了三个动画,我的标志文字和我的标志图标。然后我在文本上做了一个负旋转,在图标上做了一个正旋转。它很简单但很酷。我问题的答案有代码清单。我有,它只围绕左上角旋转。我找不到设置,无法使其围绕我的可绘图设备的中心旋转(旋转)。你能提供一些细节吗?我提供了,它只围绕左上角旋转。我找不到设置,无法使其围绕我的可绘图设备的中心旋转(旋转)。你能提供一些细节吗?
// Draw the ship with its current rotation
canvas.save();
canvas.rotate((float) mHeading++, (float) mX, mCanvasHeight
- (float) mY);
mSpinningWheelImage.setBounds(xLeft, yTop, xLeft + mSpinningWheelImageWidth, yTop
+ mSpinningWheelImageHeight);
mSpinningWheelImage.draw(canvas);
canvas.restore();
}
private Animation anim;
private Animation anim2;
private Bitmap jobs;
private Bitmap wheel;
private int jobsXOffset;
private int jobsYOffset;
private int wheelXOffset;
private int wheelYOffset;
public GraphicsView(Context context)
{
super(context);
jobs = BitmapFactory
.decodeResource(getResources(), R.drawable.spinning_button);
jobsXOffset = jobs.getWidth() / 2;
jobsYOffset = jobs.getHeight() / 2;
wheel = BitmapFactory
.decodeResource(getResources(), R.drawable.wheel);
wheelXOffset = jobs.getWidth() / 2;
wheelYOffset = jobs.getHeight() / 2;
}
private void createAnim(Canvas canvas)
{
anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
.getHeight() / 2);
anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(10000L);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
startAnimation(anim);
}
private void createAnim2(Canvas canvas)
{
anim2 = new RotateAnimation(0, 360, canvas.getWidth() / 2+30, canvas
.getHeight() / 2);
anim2.setRepeatMode(Animation.INFINITE);
anim2.setRepeatCount(Animation.INFINITE);
anim2.setDuration(10000L);
anim2.setInterpolator(new AccelerateDecelerateInterpolator());
startAnimation(anim);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// creates the animation the first time
if (anim == null) {
createAnim(canvas);
}
int centerX = canvas.getWidth() / 2;
int centerY = canvas.getHeight() / 2;
canvas.drawBitmap(jobs, centerX - jobsXOffset,
centerY - jobsYOffset, null);
canvas.drawBitmap(wheel, centerX - wheelXOffset + 30,
centerY - wheelYOffset, null);
}