Android自定义View实现很简单:
1、继承View,重写构造函数、onDraw,(onMeasure)等函数。
2、如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。
3、在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".
4、在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。
实例:
自定义TextView类:
package com.zst.service.component;
import com.example.hello_wangle.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MyTextView extends TextView {
//不能在布局文件中使用
public MyTextView(Context context) {
super(context);
}
//布局文件中用到此构造函数
public MyTextView(Context content, AttributeSet attrs){
super(content, attrs);
Paint paint = new Paint();
TypedArray array = content.obtainStyledAttributes(attrs, R.styleable.MyView);
int color = array.getColor(R.styleable.MyView_textColor, 0xFF00FF00);
float size = array.getDimension(R.styleable.MyView_textSize, 36);
paint.setColor(color);
paint.setTextSize(size);
Log.i("MyTextView", "color:" + color + "\t, size:" + size);
array.recycle();
}
//修改背景颜色
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.YELLOW);
}
}
自定义计数器View:
package com.zst.service.component;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
public class CounterView extends View implements OnClickListener{
private Paint paint;
private Rect bounds;
private int mCount;
public CounterView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
bounds = new Rect();
setOnClickListener(this);
}
@Override
public void onClick(View v) {
mCount++;
invalidate(); //导致视图重绘,会调用onDraw方法
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.BLUE);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
paint.setColor(Color.RED);
paint.setTextSize(30);
String text = mCount + "";
paint.getTextBounds(text, 0, text.length(), bounds);
int textWidth = bounds.width();
int textHeight = bounds.height();
canvas.drawText(text, getWidth()/2-textWidth/2, getHeight()/2+textHeight/2, paint);
}
}
相应的属性文件attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="textValue" format="string" />
</declare-styleable>
</resources>
在布局文件中使用activity_new.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/com.example.hello_wangle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/definedTextView"
android:text="@string/app_name"/>
<com.zst.service.component.CounterView
android:layout_width="100dp"
android:layout_height="100dp"
/>
<com.zst.service.component.MyTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
my:textColor="#FFFFFFFF"
my:textSize="38sp"
/>
</LinearLayout>
NewViewActivity.java:
package com.example.hello_wangle;
import android.app.Activity;
import android.os.Bundle;
public class NewViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_new);
//setContentView(new MyTextView(this));
setContentView(R.layout.activity_new);
}
}
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISPrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdsATOfd3bkFGazxCMx8VesATMfhHLlN3XnxCMwEzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-YWan5iZwYWZzImN3IzYxMjZiBTO1MjMwcDO2QjZkRGMyQGM58CX0EzLchDMxIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjL4M3Lc9CX6MHc0RHaiojIsJye.gif)