View中的(0,0)坐标
public class MyView extends View{ Paint paint; //定义一个画笔 public MyView(Context context){ super(context); } // 初始化画笔 public void intiPaint(){ paint = new Paint(); // 设置画笔 paint.setColor(Color.GREEN); paint.setAntiAlias(true); paint.setStrokeWidth(5); // 设置线宽 } protected void onDraw(Canvas canvas){ intiPaint(); Resources rec = getResources(); BitmapDrawable bitmapDrawable = (BitmapDrawable)rec.getDrawable(R.drawable.ss); Bitmap bitmap = bitmapDrawable.getBitmap(); canvas.drawColor(Color.YELLOW); canvas.drawBitmap(bitmap, 20, 20,paint); canvas.drawLine(0, 0, this.getWidth(), this.getHeight(), paint); } /** * 这个是在XML中初始化用的 * */ public MyView(Context context,AttributeSet attrs){ super(context, attrs); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <view class="com.example.customview.MyView" android:layout_width="100dip" android:layout_height="50dip" /> </LinearLayout>
效果图:
