我想在右侧滑动旋转瓶子。它在 棒棒糖 中运行良好,但在 marshmallow 中运行不顺畅。图像的大小为100 * 100.
请让我知道错误,我该如何解决。
以下是我的代码:
GestureDetector.OnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// clearAnimation();
int sanctity = 50;
//
Speed = Math.round(velocityX / 1000);
if ((e2.getX() - e1.getX()) > sanctity) {
swipeLayout.setVisibility(View.GONE);
scroller.fling(0, 0, (int) Math.hypot(velocityX, velocityY), 0, 0, Integer.MAX_VALUE, 0, 0);
invalidate();
}
return true;
}
};
@Override
public void computeScroll() {
if (scroller.computeScrollOffset()) {
firstime = false;
float current = scroller.getCurrX();
Log.d("computeScroll ", String.valueOf(current));
setRotation(current * FACTOR);
invalidate();
}
}
答案 0 :(得分:0)
public class MainActivity extends Activity{
Button buttonClick;
ImageView img;
Bitmap source;
float angle=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img= (ImageView) findViewById(R.id.imgView);
// Create Bitmap object for the source image
source=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/Rose.jpg");
img.setImageBitmap(source);
buttonClick = (Button) findViewById(R.id.bt);
buttonClick.setOnClickListener(new OnClickListener(){
public void onClick(View view){
angle+=90;
Bitmap rotatedImage=rotateImage(source,angle);
img.setImageBitmap(rotatedImage);
}
});
}
public static Bitmap rotateImage(Bitmap sourceImage, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), matrix, true);
}
}
现在你只需创建一个图像视图和按钮来触发旋转事件。这在两个版本中都很好用