平台有需求定时开关机功能可以选择星期,本来想在网上找个可以选择星期的Selector,但是好像没找到合适的,只能自己写一个了。
一个星期选择器自定义View,效果图
WeekCircleView.java
package com.fqcx.rpsettings.view;
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.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.fqcx.rpsettings.R;
/**
*
* 星期选择器
*
* */
public class WeekCircleView extends View {
public int width = 140;
public int height = 140;
private Paint circlePaint;
private Paint textPaint;
private DisplayMetrics dm;
private static final String TAG = "WeekCircleView";
private String text;
private boolean isChecked;
//三个构造函数都必须调用,否则无法正常使用自定义view
public WeekCircleView(Context context) {
this(context,null);
}
public WeekCircleView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public WeekCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WeekCircleView);
text = a.getString(R.styleable.WeekCircleView_circletext);
isChecked = a.getBoolean(R.styleable.WeekCircleView_circlecheck, false);
initPaint();
}
public void initPaint(){
circlePaint = new Paint();
circlePaint.setAntiAlias(true);
if(isChecked){
circlePaint.setColor(Color.BLUE);
}else {
circlePaint.setColor(Color.BLACK);
}
circlePaint.setStyle(Paint.Style.STROKE);
textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(44);
dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
Log.e(TAG + " WeekCircleView :", "screenWidth=" + dm.widthPixels + "; screenHeight=" + dm.heightPixels);
if(dm.widthPixels <= 1920 || dm.heightPixels <=1080){
width = 70;
height = 70;
textPaint.setTextSize(22);
}
if(isChecked){
textPaint.setColor(Color.BLUE);
}else {
textPaint.setColor(Color.BLACK);
}
textPaint.setFakeBoldText(true);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(width / 2,width / 2,width / 2-5,circlePaint);
float textWidth = textPaint.measureText(text);
float x = (width - textWidth) / 2;
Paint.FontMetrics metrics = textPaint.getFontMetrics();
//ascent是负的,descent是正的,dy是正的
float dy = -(metrics.ascent + metrics.descent) / 2;
float y = height / 2 + dy;
canvas.drawText(text,x,y,textPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(width,height);
}
public void setChecked(boolean checked){
if(checked){
textPaint.setColor(Color.BLUE);
isChecked=true;
}else {
textPaint.setColor(Color.BLACK);
isChecked=false;
}
initPaint();
invalidate();
}
public boolean isChecked(){
return isChecked;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction() ) {
case MotionEvent.ACTION_DOWN:
isChecked=!isChecked;
initPaint();
invalidate();
return true;
}
return super.dispatchTouchEvent(event);
}
}
自定义属性
<declare-styleable name="WeekCircleView">
<attr name="circletext" format="string" />
<attr name="circlecheck" format="boolean" />
</declare-styleable>
引用
<LinearLayout
android:layout_marginBottom="16dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.fqcx.rpsettings.view.WeekCircleView
android:id="@+id/checkBox_Sunday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circletext="@string/str_Sunday"/>
<com.fqcx.rpsettings.view.WeekCircleView
android:id="@+id/checkBox_Monday"
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circletext="@string/str_Monday"/>
<com.fqcx.rpsettings.view.WeekCircleView
android:id="@+id/checkBox_Tuesday"
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circletext="@string/str_Tuesday"/>
<com.fqcx.rpsettings.view.WeekCircleView
android:id="@+id/checkBox_Wednesday"
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circletext="@string/str_Wednesday"/>
<com.fqcx.rpsettings.view.WeekCircleView
android:id="@+id/checkBox_Thursday"
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circletext="@string/str_Thursday"/>
<com.fqcx.rpsettings.view.WeekCircleView
android:id="@+id/checkBox_Friday"
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circletext="@string/str_Friday"/>
<com.fqcx.rpsettings.view.WeekCircleView
android:id="@+id/checkBox_Saturday"
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circletext="@string/str_Saturday"/>
</LinearLayout>
代码比较简单这里就不细说了。