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í)開機(jī)的流程詳解
相關(guān)文章
android中實(shí)現(xiàn)手機(jī)號(hào)碼的校驗(yàn)的示例代碼
本篇文章主要介紹了android中實(shí)現(xiàn)手機(jī)號(hào)碼的校驗(yàn)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Android仿微信右上角點(diǎn)擊加號(hào)彈出PopupWindow
這篇文章主要為大家詳細(xì)介紹了Android仿微信右上角點(diǎn)擊加號(hào)彈出PopupWindow,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
java,Android:在eclipse中的快捷鍵(經(jīng)典收藏)
下面的快捷鍵是常用的,本人就本身喜好且常用的收拾一下,現(xiàn)在曬出來(lái)與大家分享,感興趣的朋友可以了解小哦2013-01-01
Android從Fragment跳轉(zhuǎn)到其他Activity的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android從Fragment跳轉(zhuǎn)到其他Activity的簡(jiǎn)單實(shí)例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Android Studio實(shí)現(xiàn)帶邊框的圓形頭像
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)帶邊框的圓形頭像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android實(shí)現(xiàn)懸浮窗全系統(tǒng)版本
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)懸浮窗全系統(tǒng)版本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android使用相機(jī)實(shí)現(xiàn)拍照存儲(chǔ)及展示功能詳解
這篇文章主要介紹了Android使用相機(jī)實(shí)現(xiàn)拍照存儲(chǔ)及展示功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
全面解析Android系統(tǒng)指紋啟動(dòng)流程
這篇文章主要介紹了全面解析Android系統(tǒng)指紋啟動(dòng)流程,對(duì)Android啟動(dòng)原理感興趣的同學(xué)可以參考下2021-04-04

