自定义View的使用

时间:2023-02-09 15:11:50

第一种:通过java代码实现自定义view添加到显示布局中

第一步:定义布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/mylinearlayout"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

</LinearLayout>

第二步:定义自定义View

public class MyView extends View {
 
    private float current_x=20;
    public float getCurrent_x() {
  return current_x;
 }
 public void setCurrent_x(float currentX) {
  current_x = currentX;
 }
 public float getCurrent_y() {
  return current_y;
 }
 public void setCurrent_y(float currentY) {
  current_y = currentY;
 }
 private float current_y=20;
 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }
 @Override
 protected void onDraw(Canvas canvas) {
  
  super.onDraw(canvas);
  Paint p=new Paint();
  p.setColor(Color.RED);
  Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.shoushi);
  canvas.drawBitmap(bitmap, this.getCurrent_x(), this.getCurrent_y(), p);
 }

}

第三步:测试类实现

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myview_main);
        LinearLayout ll=(LinearLayout)findViewById(R.id.mylinearlayout);
        final MyView mv=new MyView(this, null);
        mv.setBackgroundColor(Color.BLUE);
        mv.setLayoutParams(new LayoutParams(300, 300));
        mv.setMinimumWidth(300);
        mv.setMinimumHeight(300);
        ll.addView(mv);
        mv.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    mv.setCurrent_x(event.getX());
    mv.setCurrent_y(event.getY());
    mv.invalidate();
    return false;
   }
  });
    }

第四步:演示效果

自定义View的使用

 

第二种方式:通过在布局文件中添加标签

第一步:布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/mylinearlayout"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<sun.zone.android.test.MyView
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
>

</sun.zone.android.test.MyView>
</LinearLayout>

第二步:自定义View

public class MyView extends View {
 
    private float current_x=20;
    public float getCurrent_x() {
  return current_x;
 }
 public void setCurrent_x(float currentX) {
  current_x = currentX;
 }
 public float getCurrent_y() {
  return current_y;
 }
 public void setCurrent_y(float currentY) {
  current_y = currentY;
 }
 private float current_y=20;
 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }
 @Override
 protected void onDraw(Canvas canvas) {
  
  super.onDraw(canvas);
  Paint p=new Paint();
  p.setColor(Color.RED);
  Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.shoushi);
  canvas.drawBitmap(bitmap, this.getCurrent_x(), this.getCurrent_y(), p);
 }

}

 

第三步:测试类

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myview_main);
        final MyView mv=(MyView)findViewById(R.id.myview1);
        mv.setBackgroundColor(Color.BLUE);
      mv.setOnTouchListener(new OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
   mv.setCurrent_x(event.getX());
   mv.setCurrent_y(event.getY());
   mv.invalidate();
   return false;
  }
 });}

第四步:演示效果

自定义View的使用