自定义设置文字大小控件
1.效果图
2.自定义文字设置控件
public class TextSizeSelector extends View {
private int margingLeft;
private int margingright;
private Paint paint1;
private Paint paint2;
private Paint paint3;
private Paint paint4;
private Paint paint5;
private int lineWidth;
private int lineHeight;
private Context context;
private int width;
private int marginTop;
private int marginText;
private int circleRadius;
private int size18;
private Point point1_1;
private Point point1_2;
private Point point2_1;
private Point point2_2;
private Point point3_1;
private Point point3_2;
private Point point4_1;
private Point point4_2;
private Point point5_1;
private Point point5_2;
private Point currentPoint;
private ValueAnimator valueAnimator;
private int lastTextSize;//记录上一次的字号值;
public TextSizeSelector(Context context) {
this(context,null);
}
public TextSizeSelector(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public TextSizeSelector(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
margingLeft = dp2px(context, 24);
margingright = dp2px(context, 18);
marginTop = dp2px(context, 60);
lineWidth = dp2px(context, 1);
lineHeight = dp2px(context, 7);
marginText = dp2px(context, 12);
circleRadius = dp2px(context, 12);
// setBackgroundColor(Color.parseColor("#efefef"));//背景颜色;
//初始化操作;
//画笔;
paint1 = new Paint();
paint2 = new Paint();
paint3 = new Paint();
paint4 = new Paint();
paint5 = new Paint();//圆的画笔;
setMyPaint(paint1,16 * getScreenDensity(), context.getResources().getColor(R.color.grayish_color));
setMyPaint(paint2,18 * getScreenDensity(), context.getResources().getColor(R.color.grayish_color));
setMyPaint(paint3,20 * getScreenDensity(), context.getResources().getColor(R.color.grayish_color));
setMyPaint(paint4,22 * getScreenDensity(), context.getResources().getColor(R.color.grayish_color));
setMyPaint(paint5,22 * getScreenDensity(), Color.parseColor("#FFFFFF"));
paint5.setShadowLayer(10F,0,20, Color.parseColor("#FF0000"));
}
public void setMyPaint (Paint paint, int textSize, int color) {
paint.setStrokeCap(Paint.Cap.ROUND);//设置成圆头;
paint.setAntiAlias(true); //抗锯齿
paint.setColor(color);//画笔颜色;
paint.setStrokeWidth(lineWidth);//画笔宽度;
paint.setTextSize(textSize);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//拿到宽和高之后,宽的左右加上padding;高的上面基本不变;
//宽减去左右两边的20,一共是40,就是控件的宽度;
width = w - 2 * margingLeft - margingright;
point1_1 = new Point(margingLeft,marginTop);
point1_2 = new Point(margingLeft,lineHeight + marginTop);
point2_1 = new Point(margingLeft + width /3,marginTop);
point2_2 = new Point(margingLeft + width /3,lineHeight + marginTop);
point3_1 = new Point(margingLeft + 2*width /3,marginTop);
point3_2 = new Point(margingLeft + 2 * width /3,lineHeight + marginTop);
point4_1 = new Point(width + margingLeft,marginTop);
point4_2 = new Point(width + margingLeft,lineHeight + marginTop);
point5_1 = new Point(margingLeft,lineHeight + marginTop);
point5_2 = new Point(width + margingLeft,lineHeight + marginTop);
currentPoint = new Point();
//文章字号;
int htmlContentSize = Config.getInstance().getProject().getInt("htmlContentSize",100);
if (htmlContentSize == 87) {
currentPoint.x = point1_2.x;
currentPoint.y = point1_2.y;
}else if (htmlContentSize == 100){
currentPoint.x = point2_2.x;
currentPoint.y = point2_2.y;
}else if (htmlContentSize == 112){
currentPoint.x = point3_2.x;
currentPoint.y = point3_2.y;
}else if (htmlContentSize == 125){
currentPoint.x = point4_2.x;
currentPoint.y = point4_2.y;
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawLine(point1_1.x,point1_1.y,point1_2.x,point1_2.y,paint1);//1竖线;
canvas.drawLine(point2_1.x,point2_1.y,point2_2.x,point2_2.y,paint1);//2竖线;
canvas.drawLine(point3_1.x,point3_1.y,point3_2.x,point3_2.y,paint1);//3竖线;
canvas.drawLine(point4_1.x,point4_1.y,point4_2.x,point4_2.y,paint1);//4竖线;
canvas.drawLine(point5_1.x,point5_1.y,point5_2.x,point5_2.y,paint1);//线宽
canvas.drawText("小",point1_1.x-20,point1_1.y - marginText,paint1);
canvas.drawText("标准",point2_1.x - 52,point2_1.y - marginText,paint2);
canvas.drawText("大",point3_1.x - 29,point3_1.y - marginText,paint3);
canvas.drawText("超大",point4_1.x - 65,point4_1.y - marginText,paint4);
canvas.drawCircle(currentPoint.x,currentPoint.y,circleRadius,paint5);
}
//触摸事件;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
int downX = (int)event.getX();
int downy = (int)event.getY();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int)event.getX();
int moveY = (int)event.getX();
if (moveX >= margingLeft && moveX <= (width + margingright) ) {
if (moveX >= (currentPoint.x - circleRadius) && moveX <= (currentPoint.x + circleRadius)) {
currentPoint.x = moveX;
postInvalidate();
}
}
break;
case MotionEvent.ACTION_UP:
int upX = (int)event.getX();
int upY = (int)event.getX();
if (upX > 0 && upX < (point1_2.x + width / 6)) {
if (leve != null ) {
leve.textSizeLeve(87);
}
valueAnimator = ValueAnimator.ofInt(currentPoint.x, point1_2.x);
}else if ((upX > (point2_2.x - width / 6) && upX < (point2_2.x + width / 6) )) {
valueAnimator = ValueAnimator.ofInt(currentPoint.x, point2_2.x);
if (leve != null ) {
leve.textSizeLeve(100);
}
}else if ((upX > (point3_2.x - width / 6) && upX < (point3_2.x + width / 6) )) {
if (leve != null ) {
leve.textSizeLeve(112);
}
valueAnimator = ValueAnimator.ofInt(currentPoint.x, point3_2.x);
}else if ((upX > (point4_2.x - width / 6) && upX < (point4_2.x + width / 6) )) {
if (leve != null ) {
leve.textSizeLeve(125);
}
valueAnimator = ValueAnimator.ofInt(currentPoint.x, point4_2.x);
}
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
int animatedValue = (int)animator.getAnimatedValue();
currentPoint.x = animatedValue;
postInvalidate();
}
});
valueAnimator.setDuration(250).start();
break;
}
return true;
}
//重写测量方法;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureDimension(widthMeasureSpec), measureDimension(heightMeasureSpec));
}
private int measureDimension(int measureSpec) {
int result;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else {
result = 260;
if (mode == MeasureSpec.AT_MOST) {
result = Math.min(result, size);
}
}
return result;
}
/**
* 获取屏幕密度
*
* @return 屏幕密度
*/
public static int getScreenDensity() {
Application application = getApplication();
WindowManager windowManager = (WindowManager) application.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
return Math.round(displayMetrics.density);
}
/**
* 获取当前应用
*
* @return 当前应用
*/
public static Application getApplication() {
try {
return (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null, (Object[]) null);
} catch (Exception e) {
return null;
}
}
/**
* dp转px
*
* @param context
* @return
*/
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
//把自己的档位值返回出去;
//点击事件
private Leve leve;
public void setOnTextSizeLeveClickListener(Leve leve) {
this.leve = leve;
}
public interface Leve {
void textSizeLeve(int textsize) ;
}
}
3.布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray2_color">
<com.cccollector.news.view.textsizeselector.TextSizeSelector
android:id="@+id/textSizeSelector"
android:layout_width="match_parent"
android:layout_height="85dp"
android:background="@color/lightGray2_color"
/>
</RelativeLayout>
4.代码
TextSizeSelector textSizeSelector = (TextSizeSelector) findViewById(R.id.textSizeSelector);
textSizeSelector.setOnTextSizeLeveClickListener(new TextSizeSelector.Leve() {
@Override
public void textSizeLeve(int textsize) {
//保存当前文字大小
Config.getInstance().getProject().putInt("htmlContentSize",textsize);
}
});
友情链接
Android 设置屏幕亮度
http://blog.csdn.net/wangxiaohuhu1314/article/details/79571457一套源码编译多个APP
http://blog.csdn.net/wangxiaohuhu1314/article/details/79023514
http://blog.csdn.net/wangxiaohuhu1314/article/details/78581386