-1
任何一個可以告訴我如何箱視圖類中的onclicklistenr ..OnClickListener上視圖類
我創建的應用程序一樣,採取圖像從相機..在畫布上繪製我使用框架佈局創造了一些自定義按鈕我的看法類中。但我不能創建視圖類內部的onclicklistener ....
這裏是我的子活動,在畫布上繪製
public class MesureSizeActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
}
}
和我的視圖類
public class MyView extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
boolean calibrate=true;
int calibrate_x;
int calibrate_y;
int calibrate_radius;
//Event listener controller
boolean listener_calibrate= true;
public MyView(Context c) {
super(c);
mBitmap = MainActivity.getBitmap();
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF11);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
// mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mBitmap = MainActivity.getBitmap();
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF11);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
// mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createScaledBitmap(mBitmap, w, h, true);
mCanvas = new Canvas(mBitmap);
calibrate_x = w /2;
calibrate_y = h/2;
calibrate_radius = 50;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawCircle(calibrate_x,calibrate_y,calibrate_radius,mPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
// mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
public void onClick(View v) {
if(v.getId() == R.id.up)
{
//Do some thing
}
if(v.getId() == R.id.down)
{
//Do some thing
}
}
}
而且我的XML看起來像
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:visibility="visible"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<com.example.sizemesurment_1.MyView
android:id="@+id/DrawViewId"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.example.sizemesurment_1.MyView >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom">
<ImageButton
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="80dp"
android:onClick="ButtonOnClick"
android:src="@drawable/up" />
<ImageButton
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="120dp"
android:onClick="ButtonOnClick"
android:src="@drawable/down" />
</RelativeLayout>
</FrameLayout>
雖然我按我的錯誤,如
按鈕找不到在活動課com.example.sizemesurment_1的方法ButtonOnClick(查看) .MesureSizeActivity對視圖類android.widget.Button的onClick處理程序ID爲「上」
難道我做錯了什麼。我是一個初學者.... 請幫助.......
在此先感謝.....
海薩米爾Mangroliya感謝您的幫助....它的工作... – Haris