直到现在我通过扩展ImageView类完成了自定义视图。
现在,
我必须在屏幕上创建自定义视图和2个按钮。
我必须单独显示按钮和自定义视图。按钮不应超过自定义视图。
该怎么做..?
需要你的帮助........
我的示例代码..
package com.androiddom.customview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CustomView extends ImageView {
private Paint paint = new Paint();
private Path path = new Path();
public CustomView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(15f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Gives you x and y coordinates on the Event.
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
case MotionEvent.ACTION_UP:
XYZ etc...
break;
default:
return false;
}
postInvalidate();
return true;
}
public void clear() {
//Path path = new Path();
path.reset();
invalidate();
}
}
目前我正在通过主要活动调用customview类,如下面的方法:
MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomeView tv = (CustomeView)
setContentView(tv);
}
}
答案 0 :(得分:1)
您尝试使用XML布局如下...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_weight="1"
android:text="Button 1" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
<com.androiddom.customview
android:id="@+id/custom_view"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_margin="20dip"
android:layout_weight="1" >
</com.androiddom.customview>
</LinearLayout>
<强>更新强>
假设,在XML布局名称activity_main.xml
之上。现在,更新MainActivity.java
如下......
MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize your CustomeView object in this way
// then do what you want to do with this object
CustomeView tv = (CustomeView) findViewById(R.id.custom_view);
Button bt = (Button)findViewById(R.id.button);
btPrevious.setOnClickListener(this);
}
@Override
public void onClick(View v) {
tv.clear();
}
}
另一个更新:
创建一种新方法,清除CustomeView.java
类中的图纸视图,如下所示......
// start new drawing
public void startNew() {
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
invalidate();
}
然后在onClick()
...
@Override
public void onClick(View v) {
tv.startNew();
}
您可以从DrawingView
找到有关自定义绘图的更多帮助