這是流傳已久的故事:
某天,我打電話向你請教問題,當然是個難題,你一時想不出解決方法,我又不能拿着電話在那裏傻等,於是我們約定:等你想出辦法後打手機通知我,這樣,我就掛掉電話辦其它事情去了。過了XX分鐘,我的手機響了,你興高采烈的說問題已經搞定,應該如此這般處理。
初學者,這篇文章,第一次理解自定義回調接口,可以說這是寫給自己看的。。。
直接上碼
一個自定義的View:(別急,往下看)
package ivo_chuanzhi.myapplication_bluetooth_zhongli;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
@SuppressLint("ViewConstructor")
public class AppSingleRocker extends SurfaceView implements Callback{
private SurfaceHolder mHolder;
private Paint mPaint;
public Point mRockerPosition; // 搖桿位置
private Point mCtrlPoint;// 搖桿起始位置
private int mRudderRadius = 25;// 搖桿半徑
private int mWheelRadius = 80;// 搖桿活動範圍半徑
private int batmapHW = 160;
private int batmap2HW = 40;
int isHide = 0;
Bitmap bitmap,bitmap2;
float scale;
public SingleRudderListener listener = null; //事件回調接口
public static final int ACTION_RUDDER = 1, ACTION_ATTACK_DEVICEMOVE = 2, ACTION_STOP = 3, ACTION_ATTACK_CAMERAMOVE = 4;
public AppSingleRocker(Context context, AttributeSet attrs) {
super(context, attrs);
this.setKeepScreenOn(true);
scale = context.getResources().getDisplayMetrics().density;
mRudderRadius = dip2px(15);// 搖桿半徑
mWheelRadius = dip2px(45);// 搖桿活動範圍半徑
mCtrlPoint = new Point((mRudderRadius + mWheelRadius), (mRudderRadius + mWheelRadius));// 搖桿起始位置
batmapHW = (mWheelRadius+mRudderRadius) * 2;
batmap2HW = mRudderRadius * 2;
mHolder = getHolder();
mHolder.addCallback(this);
mPaint = new Paint();
mPaint.setAntiAlias(true);
setFocusable(true);
setFocusableInTouchMode(true);
mRockerPosition = new Point(mCtrlPoint);
setZOrderOnTop(true);
mHolder.setFormat(PixelFormat.TRANSPARENT);//設置背景透明
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.joystick_l_pad);
bitmap = Bitmap.createScaledBitmap(bitmap, batmapHW, batmapHW, false);
bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.print2);
bitmap2 = Bitmap.createScaledBitmap(bitmap2,batmap2HW,batmap2HW, false);
}
//獲取屏幕的寬度,高度和密度以及dp / px
public void getDisplayMetrics() {
}
public int dip2px(float dpValue) {
return (int)(dpValue * scale + 0.5f);
}
//public SingleRudderListener listener = null; //事件回調接口
//回調接口
public interface SingleRudderListener {
void onSteeringWheelChanged(int action, int angle);
}
//設置回調接口
public void setSingleRudderListener(SingleRudderListener rockerListener) {
listener = rockerListener;
}
int len;
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
if (isHide == 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
len = MathUtils.getLength(mCtrlPoint.x, mCtrlPoint.y, event.getX(), event.getY());
//如果屏幕接觸點不在搖桿揮動範圍內,則不處理
if(len > mWheelRadius) {
return true;
}
break;
case MotionEvent.ACTION_MOVE:
len = MathUtils.getLength(mCtrlPoint.x, mCtrlPoint.y, event.getX(), event.getY());
if(len <= mWheelRadius) {
//如果手指在搖桿活動範圍內,則搖桿處於手指觸摸位置
mRockerPosition.set((int)event.getX(), (int)event.getY());
}else{
//設置搖桿位置,使其處於手指觸摸方向的 搖桿活動範圍邊緣
mRockerPosition = MathUtils.getBorderPoint(mCtrlPoint, new Point((int)event.getX(), (int)event.getY()), mWheelRadius);
}
if(listener != null) {
float radian = MathUtils.getRadian(mCtrlPoint, new Point((int)event.getX(), (int)event.getY()));
listener.onSteeringWheelChanged(ACTION_RUDDER, getAngleCouvert(radian));
}
break;
case MotionEvent.ACTION_UP:
mRockerPosition = new Point(mCtrlPoint);
if (listener != null) {
listener.onSteeringWheelChanged(ACTION_STOP, 0);
}
break;
}
Canvas_OK();
Thread.sleep(60);
}else {
Thread.sleep(200);
}
} catch (Exception e) {
}
return true;
}
public void singleRockerUp(){
mRockerPosition = new Point(mCtrlPoint);
listener.onSteeringWheelChanged(ACTION_STOP, 0);
}
//獲取搖桿偏移角度 0-360°
private int getAngleCouvert(float radian) {
int tmp = (int)Math.round(radian/Math.PI * 180);
if(tmp < 0) {
return -tmp;
}else{
return 180 + (180 - tmp);
}
}
public void surfaceCreated(SurfaceHolder holder) {
Canvas_OK();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
// 設置是否隱藏
public void Hided(int opt)
{
isHide = opt;
Canvas_OK();
}
public void setHided(int opt){
isHide = opt;
}
/**
* 返回圓盤是否隱藏
* @return
*/
public int getIsHided(){
return isHide;
}
//繪製圖像
public void Canvas_OK(){
Canvas canvas = null;
try {
if (isHide == 0) {
canvas = mHolder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);//清除屏幕
canvas.drawBitmap(bitmap, mCtrlPoint.x - mWheelRadius - mRudderRadius, mCtrlPoint.y - mWheelRadius - mRudderRadius, mPaint);
canvas.drawBitmap(bitmap2, mRockerPosition.x - mRudderRadius, mRockerPosition.y - mRudderRadius, mPaint);
}else {
canvas = mHolder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);//清除屏幕
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(canvas != null) {
mHolder.unlockCanvasAndPost(canvas);
}
}
}
}
這個自定義的View裏面,設置了,回調接口,但具體的方法未實現:
public SingleRudderListener listener = null; //事件回調接口
//回調接口
public interface SingleRudderListener {
void onSteeringWheelChanged(int action, int angle);//具體的方法
}
//設置回調接口
public void setSingleRudderListener(SingleRudderListener rockerListener) {
listener = rockerListener;
}
在這個例子裏,在
onTouchEvent(MotionEvent event){xxxx}方法裏使用了這個回調接口的方法,但具體實現任然沒有
具體的實現這個接口,是在另一個類裏面(一般是UI線程)
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(D) Log.v(TAG2, "+++ ON CREATE +++");
System.out.println("+++ ON CREATE +++");
// seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
// textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
textView_test = (TextView) findViewById(R.id.textView_test);
textView_test2 = (TextView) findViewById(R.id.textView_test2);
/*//兩種方式監聽
seekBar1.setOnSeekBarChangeListener(this);*/
initOpenBluetooth();
initWebView();
AppSingleRocker appSingleRocker_one = (AppSingleRocker) findViewById(R.id.YaoGan_one);
AppSingleRocker appSingleRocker_two = (AppSingleRocker) findViewById(R.id.YaoGan_two);
//兩種方式,有this的,是這樣的<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Monospac821 BT';font-size:11.3pt;"><span style="color:#cc7832;">public class </span>MainActivity <span style="color:#cc7832;">extends </span>ActionBarActivity <span style="color:#cc7832;">implements </span>AppSingleRocker.SingleRudderListener {}
appSingleRocker_one.setSingleRudderListener(this);//自定義接口,實現該接口 appSingleRocker_two.setSingleRudderListener(new AppSingleRocker.SingleRudderListener() { @Override public void onSteeringWheelChanged(int action,
int angle) {//具體實現 textView_test.setText("x = " + action + " " + "y = " + angle ); } }); } @Override public void onSteeringWheelChanged(int action, int angle) { textView_test2.setText("x = " + action + " " + "y = " + angle ); }
總結:第一次理解自定義回調接口,可以說這是寫給自己看的。。。