自定义view
Posted 希小风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义view相关的知识,希望对你有一定的参考价值。
这两篇文章不可错过,是最靠谱的基础文献。总的来说,如果想完全定制,就继承与于View类;如果只是在原有控件基础上拓展,那就继承TextView、Button或者LinearLayout等。接下来,就以实例的形式,逐步掌握这方面的技能。
下面就自定义一个类,用来画一个圆圈:
- <pre class="html" name="code" snippet_file_name="blog_20140110_1_5174710" code_snippet_id="150430">import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Path.Direction;
- import android.util.AttributeSet;
- import android.view.View;
- public class GraphicsView extends View {
- //用于画路径、线条等
- private Path circle;
- //画笔
- private Paint cPaint;
- public GraphicsView(Context context) {
- super(context);
- initView();
- }
- public GraphicsView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- initView();
- }
- public GraphicsView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- // TODO Auto-generated constructor stub
- initView();
- }
- private void initView(){
- circle = new Path();
- //画一个轮廓
- circle.addCircle(150, 150, 100, Direction.CW);
- //设置画笔
- cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- cPaint.setStyle(Paint.Style.STROKE);
- cPaint.setColor(Color.LTGRAY);
- cPaint.setStrokeWidth(3);
- //设置背景
- setBackgroundResource(R.drawable.ic_launcher);
- }
- @Override
- public void draw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.draw(canvas);
- canvas.drawPath(circle, cPaint);
- }
- }
- </pre><br>
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Path.Direction;
- import android.util.AttributeSet;
- import android.view.View;
- public class GraphicsView extends View {
- //用于画路径、线条等
- private Path circle;
- //画笔
- private Paint cPaint;
- public GraphicsView(Context context) {
- super(context);
- initView();
- }
- public GraphicsView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- initView();
- }
- public GraphicsView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- // TODO Auto-generated constructor stub
- initView();
- }
- private void initView(){
- circle = new Path();
- //画一个轮廓
- circle.addCircle(150, 150, 100, Direction.CW);
- //设置画笔
- cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- cPaint.setStyle(Paint.Style.STROKE);
- cPaint.setColor(Color.LTGRAY);
- cPaint.setStrokeWidth(3);
- //设置背景
- setBackgroundResource(R.drawable.ic_launcher);
- }
- @Override
- public void draw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.draw(canvas);
- canvas.drawPath(circle, cPaint);
- }
- }
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Path.Direction; import android.util.AttributeSet; import android.view.View; public class GraphicsView extends View { //用于画路径、线条等 private Path circle; //画笔 private Paint cPaint; public GraphicsView(Context context) { super(context); initView(); } public GraphicsView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub initView(); } public GraphicsView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub initView(); } private void initView(){ circle = new Path(); //画一个轮廓 circle.addCircle(150, 150, 100, Direction.CW); //设置画笔 cPaint = new Paint(Paint.ANTI_ALIAS_FLAG); cPaint.setStyle(Paint.Style.STROKE); cPaint.setColor(Color.LTGRAY); cPaint.setStrokeWidth(3); //设置背景 setBackgroundResource(R.drawable.ic_launcher); } @Override public void draw(Canvas canvas) { // TODO Auto-generated method stub super.draw(canvas); canvas.drawPath(circle, cPaint); } }
我们是继承了View,并且实现了全部三个构造方法。在这个地方需要注意一下,如果你的view不是在xml中,而且也不打算使用什么样式,那么就无需实现带有AttributeSet attrs, int defStyleAttr这样的构造方法,因为用不到样式什么的。比如在activity中就可以这样使用:
- import android.os.Bundle;
- import android.view.Menu;
- import android.app.Activity;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new GraphicsView(context));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
import android.os.Bundle; import android.view.Menu; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GraphicsView(context)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
但是,上面这个类已经实现了所有构造方法,那么就可以在布局文件里使用了,如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <com.example.apptest.GraphicsView
- android:id="@+id/graphics"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.apptest.GraphicsView android:id="@+id/graphics" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
对应的activity就是这样:
- import android.os.Bundle;
- import android.view.Menu;
- import android.app.Activity;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
import android.os.Bundle; import android.view.Menu; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
效果看起来就是这样的:
以上是关于自定义view的主要内容,如果未能解决你的问题,请参考以下文章