android数字过长自动转化,Android自定义View之数字自动增长

第一次写文,请多指教,有何问题及改进建议都可以告诉我-.-

Idea来自金山词霸App的单词计数,下面先放图

1638d702ca75

autoNumber.gif

如上图,就是,下面开始进入自定义View

自定义View步骤

1. 自定义属性

2. 生成构造方法

3. onMeasure(可选)

4. onSizeChanged(可选)

5. onLayout(可选)

6. onDraw

我这里只重写了onSizeChanged,onMeasure和onLayout没有重写

1.自定义属性

values里面新建attrs

//变化速度

//边框颜色

//数字颜色

2.生成构造方法

public AutoNumberView(Context context) {

super(context);

}

public AutoNumberView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

//自定义属性

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoNumberView);

strokeColor = typedArray.getColor(R.styleable.AutoNumberView_stroke_color, context.getResources().getColor(R.color.colorPrimaryDark));

autoSpeed = typedArray.getInteger(R.styleable.AutoNumberView_auto_speed, 1000);

textColor = typedArray.getColor(R.styleable.AutoNumberView_text_color, context.getResources().getColor(R.color.black));

typedArray.recycle();

init();

initAnimation();

}

初始化动画和画笔

private void init() {

paint = new Paint();

paint.setColor(strokeColor);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(10);

paint.setAntiAlias(true);

textPaint = new Paint();

textPaint.setColor(textColor);

textPaint.setStyle(Paint.Style.STROKE);

textPaint.setTextAlign(Paint.Align.CENTER);

textPaint.setAntiAlias(true);

}

private void initAnimation() {

//根据属性动画值重绘数字

valueAnimator = ValueAnimator.ofFloat(0,1).setDuration(autoSpeed);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

value = (float) animation.getAnimatedValue();

invalidate();

}

});

}

3.onSizeChanged

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

int min = Math.min(w, h);

//中心点X,Y

centerX = w / 2;

centerY = h / 2;

radius = (int) (min * 0.8f / 2);

textPaint.setTextSize(radius / 2);

//计算数字位于中心点的矩形

targetRect = new Rect(-min / 2, -min / 2, min / 2, min / 2);

Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();

//中线

baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;

}

4.onDraw

@Override

protected void onDraw(Canvas canvas) {

//移动中心点

canvas.translate(centerX, centerY);

//边框

canvas.drawCircle(0, 0, radius, paint);

//数字

canvas.drawText(String.valueOf((int)(value * number)), targetRect.centerX(), baseline, textPaint);

}

5.使用方法

public class MainActivity extends AppCompatActivity {

...

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ButterKnife.bind(this);

//设置数值

autoNumberView.get(0).setNumber((int) (Math.random() * 500 + 1000));

autoNumberView.get(1).setNumber((int) (Math.random() * 500 + 1000));

autoNumberView.get(2).setNumber((int) (Math.random() * 500 + 1000));

showLoading.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//启动

for (AutoNumberView auto : autoNumberView) {

auto.startAnimation();

}

}

});

numberValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

//设置数值

value.setText("设置值:" + progress + "* Math.random() * 1000");

for (AutoNumberView auto : autoNumberView) {

auto.setNumber((int) ((Math.random() * 1000) * progress));

}

}

});

autoSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

//设置速度

speed.setText("设置速度:" + progress + "* 100");

for (AutoNumberView auto : autoNumberView) {

auto.setAutoSpeed(100 * progress);

}

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeArray 是 Android 中的一个特殊的资源类型,用于在 XML 中声明自定义 View 的属性。使用 TypeArray 可以方便地在 XML 布局中指定 View 的属性,而不需要在 Java 代码中进行硬编码。 使用 TypeArray 的步骤如下: 1. 在 res/values/attrs.xml 文件中定义自定义 View 的属性。 ```xml <resources> <declare-styleable name="MyCustomView"> <attr name="customAttr1" format="integer" /> <attr name="customAttr2" format="string" /> <attr name="customAttr3" format="boolean" /> </declare-styleable> </resources> ``` 2. 在自定义 View 的构造函数中获取 TypedArray 对象,并从中获取属性值。 ```java public class MyCustomView extends View { private int customAttr1; private String customAttr2; private boolean customAttr3; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); customAttr1 = a.getInt(R.styleable.MyCustomView_customAttr1, 0); customAttr2 = a.getString(R.styleable.MyCustomView_customAttr2); customAttr3 = a.getBoolean(R.styleable.MyCustomView_customAttr3, false); a.recycle(); } } ``` 在上面的代码中,`context.obtainStyledAttributes(attrs, R.styleable.MyCustomView)` 用于获取 TypedArray 对象,`R.styleable.MyCustomView` 是在 attrs.xml 文件中定义的自定义属性集合,`a.getInt()`、`a.getString()`、`a.getBoolean()` 用于从 TypedArray 对象中获取属性值,最后需要调用 `a.recycle()` 来回收 TypedArray 对象。 3. 在 XML 布局中使用自定义 View,并设置属性值。 ```xml <com.example.MyCustomView android:layout_width="match_parent" android:layout_height="wrap_content" app:customAttr1="123" app:customAttr2="hello" app:customAttr3="true" /> ``` 在上面的代码中,`app:customAttr1`、`app:customAttr2`、`app:customAttr3` 是在 attrs.xml 文件中定义的自定义属性名,可以在 XML 布局中使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值