程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 自定義控件-android 可旋轉控件

自定義控件-android 可旋轉控件

編輯:編程綜合問答
android 可旋轉控件

我試過 在ondraw 裡面 用 canvas.rotate 方法
然後自己寫一個 setOrientation方法 調用 ondraw方法 但是刷新時間 還有效果 都不是很好。誰能給一個 android 自定義 可旋轉的控件 可以動態旋轉 的那種。

最佳回答:


我以前寫過一個旋轉按鈕控件,是用區間進行判斷的,你可以借鑒一下:

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;
}
shen332401890
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved