今天看项目,无意中发现,有几个地方用到了小圆点,有蓝色、红色的。用于标记未读消息等。点击去发现,都是小图片,或者有人想在drawable下,用shape画一个小圆点。如果样式不一样(主要是颜色),就要有多个图片或者shape类型的xml文件?虽然一个项目中小点出现的地方不多,用图片或者shape浪费的空间可以忽略不计,不过,能省就剩吧。
我用的开发工具是 Android Studio 3.0
第一种方法:不使用自定义属性
这里,我用一种比较简单(繁琐)的方法来做,即:在布局文件中布局的时候,先不设定小点的颜色,再代码中使用的时候在设置
PointView
package com.demo.mydemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class PointView extends View {
private Paint pointPaint;
private int viewWidth;
private int viewHeight;
public PointView(Context context) {
this(context, null);
}
public PointView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public PointView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.e("chen", "PointView");
init();
}
private void init() {
Log.e("chen", "init");
pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
pointPaint.setStyle(Paint.Style.FILL);
setPointDefaultStyle();
}
//设置原点的默认属性
private void setPointDefaultStyle() {
Log.e("chen", "setPointDefaultStyle");
pointPaint.setColor(0x55ff0000);
//设置画笔粗细,单位:像素。因为画笔的样式是FILL(充满),所以画笔粗细不重要。
pointPaint.setStrokeWidth(5);
}
//设置原点的半径和颜色
public void setRadiusAndColor(int color) {
Log.e("chen", "setRadiusAndColor");
pointPaint.setColor(color);
invalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
Log.e("chen", "onSizeChanged");
Log.e("viewWidth", viewWidth + "");
Log.e("viewHeight", viewHeight + "");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("chen", "onDraw");
canvas.drawCircle(viewWidth / 2, viewHeight / 2, viewHeight / 2, pointPaint);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5500ff00"
>
<com.demo.mydemo.PointView
android:id="@+id/point"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private PointView point;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
point = findViewById(R.id.point);
point.setRadiusAndColor(0xffffff55);
}
}
附日志:
com.demo.mydemo E/chen: PointView
com.demo.mydemo E/chen: init
com.demo.mydemo E/chen: setPointDefaultStyle
com.demo.mydemo E/chen: setRadiusAndColor
com.demo.mydemo E/chen: onSizeChanged
com.demo.mydemo E/viewWidth: 30
com.demo.mydemo E/viewHeight: 30
com.demo.mydemo E/chen: onDraw
第二种方法,利用自定义属性
相比上面的方法,自定义属性,可以在布局文件中布局的时候,直接指定小圆点的样式(这里以颜色为例)
1、在res/values/styles.xml文件中,添加
<declare-styleable name="myPoint">
<attr name="myColor" format="color"/>
</declare-styleable>
2、在自定义view中使用
说明:
注意下面TypedArray相关代码。在取颜色属性的时候,用的是R.styleable.myPoint_myColor。参考上面写的自定义属性,是myPoint和myColor之间用下划线相连
PointView2
package com.demo.mydemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class PointView2 extends View {
private Paint pointPaint;
private int viewWidth;
private int viewHeight;
public PointView2(Context context) {
this(context, null);
}
public PointView2(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public PointView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.e("chen", "PointView");
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myPoint);
int color = ta.getColor(R.styleable.myPoint_myColor, 0x55ff0000);
ta.recycle();
init(color);
}
private void init(int color) {
Log.e("chen", "init");
pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
pointPaint.setStyle(Paint.Style.FILL);
pointPaint.setColor(color);
pointPaint.setStrokeWidth(5);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
Log.e("chen", "onSizeChanged");
Log.e("viewWidth", viewWidth + "");
Log.e("viewHeight", viewHeight + "");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("chen", "onDraw");
canvas.drawCircle(viewWidth / 2, viewHeight / 2, viewHeight / 2, pointPaint);
}
}
布局代码
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5500ff00"
>
<com.demo.mydemo.PointView2
xmlns:chen="http://schemas.android.com/apk/res/com.demo.mydemo"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerInParent="true"
chen:myColor="#000000"
/>
</RelativeLayout>
这里要注意3个地方:
s1、xmlns:chen=”http://schemas.android.com/apk/res/com.demo.mydemo”的结尾处是包名。
s2、在当前开发工具:Android Studio上,这个引用,要写在自定义view里面。不要写在根布局RelativeLayout那里,否则,会报错
s3、chen:myColor这个可有可无,如果不写,在自定义view里面,会取默认值作为小圆点的颜色。
颜色属性已经设置上了,调用那里,可有不用额外操作
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
日志:
com.demo.mydemo E/chen: PointView
com.demo.mydemo E/chen: init
com.demo.mydemo E/chen: onSizeChanged
com.demo.mydemo E/viewWidth: 30
com.demo.mydemo E/viewHeight: 30
com.demo.mydemo E/chen: onDraw