Android自定义View

CustomView.java

/**
 * 
 */
package com.hust.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author tuke
 *
 */
public class CustomView extends View {
	//图形类型
	int s=0;
	
    //在代码中实例化时调用这个构造函数
	public CustomView(Context context) {
		super(context);	//子类不继承父类的构造函数,实例化子类时先实例化父类,所以要先调用父类的构造函数
	}
    //从xml文件中实例化是调用这个构造函数
	public CustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint=new Paint();
		paint.setColor(Color.GREEN);
		paint.setStrokeWidth(10);
		
		switch (s) {
		case 0:
		   canvas.drawCircle(200, 200, 100, paint);
			break;
		case 1:
			canvas.drawRect(60,90,360,300, paint);
			break;
		case 2:
	         Path path=new Path();
	         path.moveTo(80, 100);
	         path.lineTo(420, 250);
	         path.lineTo(80, 350);
	         canvas.drawPath(path, paint);
	       break;

		default:
			break;
		}
	}
	
	public void changeStyle(){
		s++;
		if(s>2){
			s=0;
		}
	}

}
布局文件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hust.customview.MainActivity" >

   <com.hust.customview.CustomView 
       android:id="@+id/customview"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       />

</RelativeLayout>

package com.hust.customview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {
    CustomView customView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		customView=(CustomView) findViewById(R.id.customview);
		customView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Message msg=new Message();
				msg.what=1;
				mHandler.sendMessage(msg);
				
				/*customView.changeStyle();
				customView.invalidate();//重绘,调用onDraw、
*/			}
		});
	}

	Handler mHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				customView.changeStyle();
				customView.invalidate();//重绘,调用onDraw、
				break;
			default:
				break;
			}					
		}
		
	};
	
}

你可能感兴趣的:(Android自定义View)