package com.example.test; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class DrawView extends View{ /* * 功能:构造方法 * */ public DrawView(Context context, AttributeSet attrs) { super(context, attrs); } /* * 功能:重写onDraw方法 * */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tableLayout1" android:orientation="vertical" > <com.example.test.DrawView android:id="@+id/drawView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Paint paint=new Paint(); paint.setColor(Color.RED); paint.setShadowLayer(2, 3, 3, Color.rgb(180, 180, 180)); canvas.drawRect(40, 40,200,100,paint);
package com.example.test; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class DrawView extends View{ /* * 功能:构造方法 * */ public DrawView(Context context, AttributeSet attrs) { super(context, attrs); } /* * 功能:重写onDraw方法 * */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint=new Paint(); paint.setColor(Color.RED); paint.setShadowLayer(2, 3, 3, Color.rgb(180, 180, 180)); canvas.drawRect(40, 40,200,100,paint); } }
效果如图所示
转载请注明出处:http://blog.csdn.net/acmman/article/details/45156381