我想知道如何在android中制作可绘制图像的动画。这就是我使用LuckyWheel
库,其中有一个模型类LuckyItem
public class LuckyItem {
public String text;
public int icon;
public int color;
}
这是图像的屏幕截图
这是MainActivity
public class MainActivity extends AppCompatActivity {
List<LuckyItem> data = new ArrayList<>();
Button btn,play;
boolean flag=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.button);
play=(Button) findViewById(R.id.play);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
final LuckyWheelView luckyWheelView = (LuckyWheelView) findViewById(R.id.luckyWheel);
LuckyItem luckyItem1 = new LuckyItem();
luckyItem1.text = "100";
luckyItem1.icon = R.drawable.test1;
luckyItem1.color = 0xffFFF3E0;
data.add(luckyItem1);
LuckyItem luckyItem2 = new LuckyItem();
luckyItem2.text = "200";
luckyItem2.icon = R.drawable.test2;
luckyItem2.color = 0xffFFE0B2;
data.add(luckyItem2);
LuckyItem luckyItem3 = new LuckyItem();
luckyItem3.text = "300";
luckyItem3.icon = R.drawable.test3;
luckyItem3.color = 0xffFFCC80;
data.add(luckyItem3);
LuckyItem luckyItem4 = new LuckyItem();
luckyItem4.text = "400";
luckyItem4.icon = R.drawable.test4;
luckyItem4.color = 0xffFFF3E0;
data.add(luckyItem4);
LuckyItem luckyItem5 = new LuckyItem();
luckyItem5.text = "500";
luckyItem5.icon = R.drawable.test5;
luckyItem5.color = 0xffFFE0B2;
data.add(luckyItem5);
LuckyItem luckyItem6 = new LuckyItem();
luckyItem6.text = "600";
luckyItem6.icon = R.drawable.test6;
luckyItem6.color = 0xffFFCC80;
data.add(luckyItem6);
//////////////////
//////////////////////
LuckyItem luckyItem7 = new LuckyItem();
luckyItem7.text = "700";
luckyItem7.icon = R.drawable.test7;
luckyItem7.color = 0xffFFF3E0;
data.add(luckyItem7);
LuckyItem luckyItem8 = new LuckyItem();
luckyItem8.text = "800";
luckyItem8.icon = R.drawable.test8;
luckyItem8.color = 0xffFFE0B2;
data.add(luckyItem8);
LuckyItem luckyItem9 = new LuckyItem();
luckyItem9.text = "900";
luckyItem9.icon = R.drawable.test9;
luckyItem9.color = 0xffFFCC80;
data.add(luckyItem9);
luckyWheelView.setData(data);
luckyWheelView.setRound(getRandomRound());
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(flag)
{
play.setEnabled(false);
Log.d("ins", "called");
}
flag=false;
int index = getRandomIndex();
luckyWheelView.startLuckyWheelWithTargetIndex(index);
int te=getRandomRound();
Toast.makeText(getApplicationContext(), String.valueOf(te)+" rounds!", Toast.LENGTH_SHORT).show();
}
});
luckyWheelView.setLuckyRoundItemSelectedListener(new LuckyWheelView.LuckyRoundItemSelectedListener() {
@Override
public void LuckyRoundItemSelected(int index) {
int icons = data.get(index - 1).icon;
Toast.makeText(getApplicationContext(), String.valueOf(index), Toast.LENGTH_SHORT).show();
flag=true;play.setEnabled(true);
}
});
}
private int getRandomIndex() {
Random rand = new Random();
return rand.nextInt(data.size())+1;
}
private int getRandomRound() {
Random rand = new Random();
return rand.nextInt(5)+9;
}
}
当轮选择任何项目时,我想为扇区的图标设置动画。图像是一个可绘制的文件。
我尝试使用Tween Animation,但我只能从滚轮中心设置可绘制(图标)的动画。我想从原始位置为图标设置动画。
luckyWheelView.setLuckyRoundItemSelectedListener(index -> {
if (!isAdded()) {
return;
}
Bitmap icons = BitmapFactory.decodeResource(getResources(), data.get(index - 1).icon);
final ImageView imageView = (ImageView) view.findViewById(R.id.images);
imageView.setImageBitmap(icons);
scale = AnimationUtils.loadAnimation(getActivity(), R.anim.scale);
translate = AnimationUtils.loadAnimation(getActivity(), R.anim.translate);
final AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(scale);
s.addAnimation(translate);
imageView.startAnimation(s);
s.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
flag = true;
});
translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1500"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="7%p"
android:toYDelta="-45%p" />
</set>
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5">
</scale>
</set>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/lucky_wheel_background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<FrameLayout
android:id="@+id/luckyWheel_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<rubikstudio.library.LuckyWheelView
android:id="@+id/luckyWheel"
android:layout_width="@dimen/dp333"
android:layout_height="@dimen/dp333"
android:layout_centerInParent="true"
app:lkwBackgroundColor="#FF9800"
app:lkwCursor="@drawable/ic_spin_pointer"
app:lkwTextColor="#263238">
</rubikstudio.library.LuckyWheelView>
</FrameLayout>
<ImageView
android:id="@+id/images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true" />
<ImageButton
android:id="@+id/play_btn"
android:layout_width="@dimen/dp105"
android:layout_height="@dimen/dp57"
android:layout_below="@+id/luckyWheel_frame"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="@drawable/ic_spin_button" />
</RelativeLayout>