我有一个以像素为单位的png图像宽度200 x高度50.我需要绕其中心旋转一个角度.
我的onDraw方法
@OverrIDe public voID onDraw(Canvas canvas){ initDrawingTools(); drawRect(canvas); drawBack(canvas); Matrix mat = new Matrix(); Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.needle); cX = getWIDth()/2-bMap.getWIDth()/2; cY = getHeight()/2-bMap.getHeight()/2; mat.setTranslate(cX, cY); mat.postRotate(angleSpeed,cX, cY); Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,bMap.getWIDth(),bMap.getHeight(), mat, true); canvas.drawBitmap(bMapRotate, mat, null); }@H_419_8@
这是我管理的最接近的.据我了解,图像的中心在旋转时是漂浮的.例如:0度-200×50、90度50×200等.在这种情况下,它不会绕其中心旋转.有人可以给我一些提示或解释如何获得结果吗?
编辑工作的Mikel Pascualc建议:
动画后如何使箭头保持在倾斜位置???
seekbar = (Seekbar)findVIEwByID(R.ID.seekbar1); seekbar.setonSeekbarchangelistener(new OnSeekbarchangelistener() { @OverrIDe public voID onStopTrackingtouch(Seekbar seekbar) { ROTATE_TO = speed; spin();} @OverrIDe public voID onStartTrackingtouch(Seekbar seekbar) { ROTATE_FROM = speed;} @OverrIDe public voID onProgressChanged(Seekbar seekbar, int progress, boolean fromUser) { speed = progress;// textVIEw = (TextVIEw)findVIEwByID(R.ID.textVIEw1);// textVIEw.setText(progress); //rodykle.onSpeedChanged((float)progress); } }); } public voID spin(){ TextVIEw textVIEw1 = (TextVIEw)findVIEwByID(R.ID.textVIEw1); textVIEw1.setText("From: " + ROTATE_FROM + "\nTo: " + ROTATE_TO + "\nProgress: " + speed); ImageVIEw needle = (ImageVIEw) findVIEwByID(R.ID.needle1); RotateAnimation r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.relative_TO_SELF, 0.5f, Animation.relative_TO_SELF, 0.5f); r.setDuration((long) 2*1000); r.setRepeatCount(0); r.setFillAfter(true); // <-- ADDED THIS TO STAY img IN ANGLE AFTER ANIMATION needle.startAnimation(r); }@H_419_8@
解决方法:
您最好使用动画.
例:
public class NeedleRotateActivity extends Activity {private static final float ROTATE_FROM = 0.0f;private static final float ROTATE_TO = -10.0f * 360.0f;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); ImageVIEw needle = (ImageVIEw) findVIEwByID(R.ID.needle); RotateAnimation r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.relative_TO_SELF, 0.5f, Animation.relative_TO_SELF, 0.5f); r.setDuration((long) 2*1500); r.setRepeatCount(0); r.setFillAfter(true); needle.startAnimation(r);}@H_419_8@
总结 以上是内存溢出为你收集整理的android-围绕其中心旋转自定义图像全部内容,希望文章能够帮你解决android-围绕其中心旋转自定义图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)