Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android RatingBar调整字体大小

Android自定义控件RatingBar调整字体大小

作者:rome753

这篇文章主要为大家详细介绍了Android自定义控件RatingBar调整字体大小的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

项目需要,做一个可以调整字体大小的控件,能在滑动或点击时改变选中的位置,效果图如下:

这是一个类似于RatingBar的控件,然而配置RatingBar的样式难以实现这样的效果,如选中的图案和上面的文字对齐。因此,有必要写一个自定义View来实现。

思路如下:

TextRatingBar类代码如下:

package cc.rome753.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by rome753 on 2017/3/10
 */

public class TextRatingBar extends View{

 //paddingLeft
 private int mLeft;
 //paddingTop
 private int mTop;
 //当前rating
 private int mRating;
 //总raring数
 private int mCount;
 //rating文字
 private String[] texts = {"小","中","大","超大"};
 //相邻raring的距离
 private int mUnitSize;
 //bar到底部的距离
 private int mYOffset;
 //小竖条的一半长度
 private int mMarkSize;

 Paint paint = new Paint();

 public TextRatingBar(Context context) {
 this(context, null);
 }

 public TextRatingBar(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }

 public TextRatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 mCount = 4;
 mRating = 0;
 mMarkSize = 3;
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 Log.i("test", getMeasuredWidth() + " " + getMeasuredHeight());
 mLeft = (getPaddingLeft()+getPaddingRight())/2;
 mTop = getPaddingTop();
 int barWidth = getMeasuredWidth() - 2 * mLeft;
 mUnitSize = barWidth/(mCount - 1);
 mYOffset = getMeasuredHeight() - getPaddingBottom();
 }

 @Override
 protected void onDraw(Canvas canvas) {
 paint.setStrokeWidth(2);
 paint.setColor(Color.RED);
 canvas.drawLine(mLeft,mYOffset,mLeft+mRating*mUnitSize,mYOffset,paint);
 for(int i=0;i<mCount;i++){
  paint.setColor(Color.RED);
  canvas.drawLine(mLeft+i*mUnitSize,mYOffset-mMarkSize,mLeft+i*mUnitSize,mYOffset+mMarkSize,paint);
  paint.setColor(mRating == i ? Color.RED : Color.BLACK);
  paint.setTextSize(30);
  paint.setTextAlign(Paint.Align.CENTER);
  canvas.drawText(texts[i],mLeft+i*mUnitSize,mTop,paint);
 }
 paint.setColor(Color.GRAY);
 canvas.drawLine(mLeft+mRating*mUnitSize,mYOffset,mLeft+(mCount-1)*mUnitSize,mYOffset,paint);
 canvas.drawCircle(mLeft+mRating*mUnitSize,mYOffset,10,paint);

 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE){
  float x = event.getX();
  for(int i=0;i<mCount;i++){
  float distance = mLeft+i*mUnitSize - x;
  if(Math.abs(distance) < 100){
   setRating(i);
   if(onRatingListener != null){
   onRatingListener.onRating(mRating);
   }
   break;
  }
  }
 }
 return true;
 }

 public void setRating(int rating) {
 mRating = rating;
 invalidate();
 }

 private OnRatingListener onRatingListener;

 public void setOnRatingListener(OnRatingListener onRatingListener) {
 this.onRatingListener = onRatingListener;
 }

 interface OnRatingListener{
 void onRating(int rating);
 }
}

几个要点:

没有用到自定义属性,使用时直接放到布局中,周围加上padding就行了。

 <cc.rome753.demo.view.TextRatingBar
 android:paddingTop="20dp"
 android:paddingLeft="40dp"
 android:paddingRight="40dp"
 android:paddingBottom="35dp"
 android:layout_width="match_parent"
 android:layout_height="70dp" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文