2 shen332401890 shen332401890 于 2012.12.12 13:06 提问

android 可旋转控件

我试过 在ondraw 里面 用 canvas.rotate 方法
然后自己写一个 setOrientation方法 调用 ondraw方法 但是刷新时间 还有效果 都不是很好。谁能给一个 android 自定义 可旋转的控件 可以动态旋转 的那种。

2个回答

huweilong1030
huweilong1030   2012.12.14 16:16
已采纳

我以前写过一个旋转按钮控件,是用区间进行判断的,你可以借鉴一下:

enter code hpackage 

jxt.app.radio.view;

import jxt.app.radio.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class FMView extends ViewGroup {

public float fRotateNum;
float fCenterX,fCenterY;
private int iPic = R.drawable.switch_fm_s1;

public FMView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public FMView(Context context) {
    super(context);
}


protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {

}


protected void onDraw(Canvas canvas) {

// super.onDraw(canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), iPic);

    RectF target = new RectF();
    target.left = 0;
    target.top = 0;
    target.right = bitmap.getWidth();
    target.bottom = bitmap.getHeight();
    fCenterX = (int) (target.right/2.0);
    fCenterY = (int) (target.bottom/2.0);
    canvas.save();
    canvas.rotate(fRotateNum, fCenterX, fCenterY);
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
    canvas.drawBitmap(bitmap, null, target, null);
    canvas.restore();
    bitmap.recycle();
}

public float getfRotateNum() {
    return fRotateNum;
}

public void setfRotateNum(float fRotateNum) {
    this.fRotateNum = fRotateNum;
}

public float getfCenterX() {
    return fCenterX;
}

public void setfCenterX(float fCenterX) {
    this.fCenterX = fCenterX;
}

public float getfCenterY() {
    return fCenterY;
}

public void setfCenterY(float fCenterY) {
    this.fCenterY = fCenterY;
}

}
ere

触发这个控件的旋转:

    private float fFirstX,fFirstY,fCenterX,fCenterY,fSecondX,fSecondY;
private float fLastRotateFM,fRotateFM,fRotateVoice;

public boolean onTouch(View view, MotionEvent event) {
    if(view.getId() == R.id.image_pinlv){
        hideRecordLayout();
        if(bRecording == true){
            showRecordDialog();
        }else{
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                fCenterX = mFMView.getfCenterX();
                fCenterY = mFMView.getfCenterY();
                fFirstX = event.getX();
                fFirstY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if(fFirstX != fCenterX){
                    fFirstAngle = (float) Math.toDegrees(Math.atan(Math.abs(fFirstY-fCenterY)/Math.abs(fFirstX-fCenterX)));
                }
                fSecondX = event.getX();
                fSecondY = event.getY();
                if(fSecondX != fCenterX){
                    fSecondAngle = (float) Math.toDegrees(Math.atan(Math.abs(fSecondY-fCenterY)/Math.abs(fSecondX-fCenterX)));
                }
                if(fFirstX >= fCenterX && fFirstY <= fCenterY){     //在第一区间,即右上角
                    fRotateFM += fFirstAngle - fSecondAngle;
                }else if(fFirstX < fCenterX && fFirstY <= fCenterY){  //在第二区间,即左上角
                    fRotateFM += fSecondAngle - fFirstAngle;
                }else if(fFirstX < fCenterX && fFirstY > fCenterY){  //在第三区间,即左下角
                    fRotateFM += fFirstAngle - fSecondAngle;
                }else if(fFirstX > fCenterX && fFirstY > fCenterY){  //在第四区间,即右下角
                    fRotateFM += fSecondAngle - fFirstAngle;
                }
                if(fRotateFM != 0){
                    mFMView.setfRotateNum(fRotateFM);
                    mFMView.invalidate();
                }
                fFirstX = fSecondX;
                fFirstY = fSecondY;
                break;
            case MotionEvent.ACTION_UP:
                if(fRotateFM - fLastRotateFM > 0){
                    //往右旋转
                }else{
                    //往左旋转
                }
                break;
            }
        }
    }
    return true;
}
suannai0314
suannai0314 这个答案能解决你的问题么?可以的话给采纳一下吧。
5 年多之前 回复
shen332401890
shen332401890 谢谢
5 年多之前 回复
cytown
cytown   2012.12.12 13:59

ProgressBar 设置为indeterminate的时候就是你需要的动态旋转,自己去找代码就行。

shen332401890
shen332401890 不是一直转,一直转 用动画就OK 要能停在那个方向的那种
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!