Android使用自定義view在指定時(shí)間內(nèi)勻速畫一條直線的實(shí)例代碼
本文講述了Android使用自定義view在指定時(shí)間內(nèi)勻速畫一條直線的實(shí)例代碼。分享給大家供大家參考,具體如下:
1.效果圖:

2.自定義view實(shí)現(xiàn)
public class UniformLine extends View {
private int x, y, nextX, nextY, incrementY, incrementX;
public UniformLine(Context context) {
super(context);
}
public UniformLine(Context context, int x, int y, int nextX, int nextY) {
super(context);
this.x = x;
this.y = y;
this.nextX = nextX;
this.nextY = nextY;
init();
}
private void init() {
p = new Paint();
p.setColor(Color.WHITE);
p.setAntiAlias(true);
p.setStrokeWidth(4.0f);
ValueAnimator valueAnimatorX = ValueAnimator.ofFloat(x, nextX);
valueAnimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
incrementX = Math.round((Float) animation.getAnimatedValue());
invalidate();
}
});
ValueAnimator valueAnimatorY = ValueAnimator.ofInt(y, nextY);
valueAnimatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
incrementY = (int) animation.getAnimatedValue();
invalidate();
}
});
AnimatorSet animatorSet = new AnimatorSet();
LinearInterpolator ll = new LinearInterpolator();
animatorSet.setInterpolator(ll);//勻速
animatorSet.setDuration(2000);
animatorSet.playTogether(valueAnimatorX, valueAnimatorY);
animatorSet.start();
}
Paint p;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(Util.Div(Math.round(x)), Util.Div(Math.round(y)),
Util.Div(Math.round(incrementX)), Util.Div(Math.round(incrementY)), p);// 斜線
}
}
3.調(diào)用
uniformLine = new UniformLine(mContext, 300, 500, 600, 200); addView(uniformLine);
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- android實(shí)現(xiàn)定時(shí)拍照并發(fā)送微博功能
- android實(shí)現(xiàn)定時(shí)拍照功能
- Android使用Handler實(shí)現(xiàn)定時(shí)器與倒計(jì)時(shí)器功能
- Android定時(shí)器和倒計(jì)時(shí)實(shí)現(xiàn)淘寶秒殺功能
- Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果
- android service實(shí)現(xiàn)循環(huán)定時(shí)提醒功能
- android用鬧鐘定時(shí)做http請(qǐng)求推送的解決方案
- Android鬧鐘機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)功能
- Android編程實(shí)現(xiàn)popupwindow定時(shí)消失的方法
- Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
- 詳解Android實(shí)現(xiàn)定時(shí)器的幾種方法
- Android定時(shí)開(kāi)機(jī)的流程詳解
相關(guān)文章
Android adb logcat 命令查看日志詳細(xì)介紹
這篇文章主要介紹了Android adb logcat 命令詳細(xì)介紹的相關(guān)資料,這里對(duì)logcat 命令進(jìn)行了詳細(xì)介紹,并介紹了過(guò)濾日志輸出的知識(shí),需要的朋友可以參考下2016-12-12
Android之利用EventBus發(fā)送消息傳遞示例
本篇文章主要介紹了Android之利用EventBus進(jìn)行消息傳遞示例。EventBus是一款針對(duì)Android優(yōu)化的發(fā)布/訂閱事件總線,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Flutter最小刷新范圍探索ValueListenableBuilder使用詳解
這篇文章主要為大家介紹了Flutter最小刷新范圍探索ValueListenableBuilder使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫功能
這篇文章主要介紹了Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫功能,感興趣的小伙伴們可以參考一下2015-12-12
Android MonoRepo多倉(cāng)和單倉(cāng)的差別理論
這篇文章主要為大家介紹了Android MonoRepo多倉(cāng)和單倉(cāng)的差別理論,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù)
本篇文章主要介紹了Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù),我們可以通過(guò)TextWatcher去觀察輸入框中輸入的內(nèi)容,有興趣的可以了解一下。2017-04-04

