我有一张箭头图片,我想让它从0度旋转到180度(就像仪表盘上的指针)。箭头的一个点固定在屏幕中央和底部,箭头头部应该移动。箭头的长度是固定的(它是图像)。同时我有两个按钮,当按下左按钮时箭头向左转,按下右按钮时箭头向右转。
这个过程的逻辑是什么?
这个过程的逻辑是什么?
![enter image description here](https://istack.dev59.com/b8B7u.webp)
如果你使用画布来绘制(在你的情况下应该这样做),这实际上非常简单。
假设你知道图像应该旋转的点的坐标,那么可以像这样操作:
private void doDraw(Canvas canvas) {
canvas.save();
float px = ...;
float py = ...;
canvas.rotate(degrees, px, py);
arrow.draw(canvas);
canvas.restore();
}
degrees将是一个整数值,当用户点击左右按钮时,您需要对其进行增加/减少。canvas.rotate会处理其余部分!
你需要在安卓平台下使用3D旋转完成动画,并尝试使用矩阵旋转...我已经有了相应的位图代码。
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.bar);
Matrix mtx = new Matrix();
mtx.postRotate(180); // rotating 180 degrees clockwise
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth() ,bmp.getHeight() , mtx, true); // creating the bitmap image with new angle
还要检查这个
ImageView arrow = findViewById(/* id of your arrow */);
int width = arrow.getWidth();
int height = arrow.getHeight();
newAngle = /* init it by needed angle */;
RotateAnimation animation = new RotateAnimation(oldAngle, newAngle, width / 2, height);
oldAngle = newAngle;
animation.setDuration(200); // you may set another duration
animation.setFillAfter(true);
arrow.startAnimation(animation);
祝你好运!
我看到了this的例子,然后我进行了一些调整,使其更或多或少地符合您的需求,请查看示例以更好地理解我的代码 :)
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class TesteRotateActivity extends Activity implements
SeekBar.OnSeekBarChangeListener {
private ImageView myImageView;
private SeekBar seekbarRotate;
private float curScale = 1F;
private float curRotate = 0F;
private Bitmap bitmap;
private int 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);
seekbarRotate = (SeekBar) findViewById(R.id.rotate);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.teste);//here you insert your image
bmpHeight = bitmap.getHeight();
drawMatrix();
seekbarRotate.setOnSeekBarChangeListener(this);
}
private void drawMatrix() {
Matrix matrix = new Matrix();
Matrix matrixb = new Matrix();
Matrix matrixc = new Matrix();
matrix.postScale(curScale, curScale);
matrixb.setRotate(curRotate, 100, bmpHeight);
matrixc.setTranslate(100, 0);
matrix.setConcat(matrixb, matrixc);
Bitmap targetBitmap = Bitmap.createBitmap(200, bmpHeight,
bitmap.getConfig());
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(bitmap, matrix, new Paint());
myImageView.setImageBitmap(targetBitmap);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
curRotate = (float) progress;
drawMatrix();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
主要的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center" />
</LinearLayout>