前兩篇博客:
Android 弧形轉盤的實現(二),列表自動選中;RecyclerView滑動後自動選中居中的條目,RecyclerView實現WheelView效果;
已經大致實現了弧形轉盤的效果,還有一個動畫需要做,這個比較簡單;
效果圖左邊的刻度輪盤是個半圓,其實應該是個整圓只顯示一半,另外一半在屏幕外面;
嘗試使用PaddingLift把ImageView擠到屏幕外面,默認效果是可以,但是旋轉的時候也是個半圓在旋轉,這樣就不行了;索性直接設置IamgeView是整個圓,然後做平移動畫,移動一半位置到屏幕外面;
1、平移,只顯示半圓;
ImageView控件時固定寬高的 400*400的 , 直接X軸移動-200dp;
iv = findViewById(R.id.iv);
setTranslationX(iv , UiUtils.dip2px(this , 200)*-1);
public void setTranslationX(View view , float dy) {
ObjectAnimator translationYAnimator = ObjectAnimator.ofFloat(view,"translationX",0f,dy);
translationYAnimator.setDuration(1L);
translationYAnimator.start();
}
2、ImageView輪盤跟隨RecyclerView滑動做旋轉動畫;
一定要用float類型 , 不然會丟失精度;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
float dyy = dy*-1;
setRotate(dyy/4);
}
});
private float values = 0f;
private float newValues = 0f;
private ObjectAnimator rotationAnimator;
public void setRotate(float dy) {
if (rotationAnimator == null)
rotationAnimator = ObjectAnimator.ofFloat(iv,"rotation",0f,10f);
newValues = values + dy;
rotationAnimator.setDuration(10L);
rotationAnimator.setFloatValues(values,newValues);
rotationAnimator.start();
values = newValues;
}
完工,看效果! (沒有輪盤圖片,就用的Android的默認啓動圖標;)
圖片有點大,加載可能稍微慢一點,GIF圖感覺卡卡的,想體驗效果還請下載代碼自己運行起來看看;
代碼已上傳:https://github.com/CuiChenbo/ArcSelectList , 歡迎Star