Jul*_* di 4 android android-animation android-layout android-graphics
我想为我的项目制作一个星形。我搜索了这么多,现在我终于可以使用画布制作星形了。
但接下来我想要的对我来说真的很难。真是噩梦。我尝试并搜索过,但没有任何效果对我有用。
我想要的是星星必须由没有背景颜色的线条组成。唯一的线.. 越线..
然后一行被表达并淡出,下一行被创建并再次淡出,然后该行被创建并再次淡出,我希望它是永久的这个动作。直到我将这个活动的屏幕留给另一个。
这段代码取自我前段时间从事的一个项目。我认为它可以满足您的需求。
首先,我不知道你是如何为星星创建线条的。我只是使用矢量可绘制对象来创建星形线(有关如何使用 VectorDrawables 的更多详细信息,请查看这篇文章)。我应该警告你,我做的很匆忙,所以它不会很漂亮
警告: 这篇文章会很长
star1.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path android:pathData="M20,24L12,0"
android:strokeColor="@color/starColor"
android:strokeWidth="0.1"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
star2.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path android:pathData="M4,24L12,0"
android:strokeColor="@color/starColor"
android:strokeWidth="0.1"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
star3.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path android:pathData="M0,8L24,8"
android:strokeColor="@color/starColor"
android:strokeWidth="0.1"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
star4.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path android:pathData="M4,24L24,8"
android:strokeColor="@color/starColor"
android:strokeWidth="0.1"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
star5.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path android:pathData="M20,24L0,8"
android:strokeColor="@color/starColor"
android:strokeWidth="0.1"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
好的。现在您已拥有所有必需的可绘制对象,让我们跳转到 Activity XML 并创建布局。这里只是基本的东西。我将使用 5 ImageViews 来保持不同的星线(这使得动画很方便,但可能会导致我们稍后讨论的性能问题)。我将使用 aConstraintLayout作为根视图。
活动测试.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:alpha="0"/>
<ImageView
android:id="@+id/image2"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:alpha="0"/>
<ImageView
android:id="@+id/image3"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:alpha="0"/>
<ImageView
android:id="@+id/image4"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:alpha="0"/>
<ImageView
android:id="@+id/image5"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:alpha="0"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,我没有将任何可绘制对象分配给ImageViewusingandroid:src属性。我鼓励你继续尝试这样做。同时,我正在以编程方式添加图像。
在onCreate方法上方声明以下变量
private ImageView image1, image2, image3, image4, image5;
private Context context;
private int i = 1;
private long duration = 800;
private Handler animHandler;
Run Code Online (Sandbox Code Playgroud)
animHandler将负责保持动画运行。我将i用来跟踪ImageView要制作动画的内容。顾名思义,该字段duration将保存动画所需的持续时间。
这是代码的其余部分。我会尽可能提供意见。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Initialising the imageview. I'm using SDK 26 so I did not
// require typecasting.
image1 = findViewById(R.id.image1);
image2 = findViewById(R.id.image2);
image3 = findViewById(R.id.image3);
image4 = findViewById(R.id.image4);
image5 = findViewById(R.id.image5);
context = TestActivity.this;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setupView(); // This method will initialize all IVs
// and add the vector drawable as bitmap
animHandler = new Handler();
startAnim();
}
}, 200);
}
Run Code Online (Sandbox Code Playgroud)
设置视图
private void setupView() {
setVectorDrawable(image1, ContextCompat.getDrawable(context,R.drawable.star1));
setVectorDrawable(image2, ContextCompat.getDrawable(context,R.drawable.star2));
setVectorDrawable(image3, ContextCompat.getDrawable(context,R.drawable.star3));
setVectorDrawable(image4, ContextCompat.getDrawable(context,R.drawable.star4));
setVectorDrawable(image5, ContextCompat.getDrawable(context,R.drawable.star5));
}
Run Code Online (Sandbox Code Playgroud)
设置向量可绘制
private void setVectorDrawable(ImageView imageView, Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(imageView.getWidth(),
imageView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
imageView.setImageBitmap(bitmap);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我创建了 5 个不同的位图来保存 5 行。这还不是什么大问题。但是对于较大的项目(我必须同时使用 69 个位图),在内存中使用大量位图可能不是一个好习惯。5 个位图应该可以正常工作。
开始动画
private void startAnim() {
runnable.run();
}
Run Code Online (Sandbox Code Playgroud)
可运行的
Runnable runnable = new Runnable() {
@Override
public void run() {
switch (i) {
case 1:
animateStarIn(image1);
break;
case 2:
animateStarIn(image2);
break;
case 3:
animateStarIn(image3);
break;
case 4:
animateStarIn(image4);
break;
case 5:
animateStarIn(image5);
break;
case 6:
animateStartOut(image1);
break;
case 7:
animateStartOut(image2);
break;
case 8:
animateStartOut(image3);
break;
case 9:
animateStartOut(image4);
break;
case 10:
animateStartOut(image5);
break;
}
i++;
if (i == 11) i = 1;
animHandler.postDelayed(runnable, duration);
}
};
private void animateStarIn(ImageView imageView) {
imageView.animate().alpha(1).setDuration(duration).setInterpolator(new AccelerateInterpolator());
}
private void animateStartOut (ImageView imageView) {
imageView.animate().alpha(0).setDuration(duration).setInterpolator(new DecelerateInterpolator());
}
Run Code Online (Sandbox Code Playgroud)
简而言之,我正在创建一个 runnable,它将ImageViews根据值变量i持有的不同以不同的方式进行动画处理。
让我再次强调一下,我已经发布了我在一段时间前从事的一个项目中的代码。即使它不能直接满足您的要求,我也鼓励您尝试并尝试一下以使其正常工作。
编辑
这个动画通过确保runnable重复来工作。停止动画使用
animHandler.removeCallbacks(runnable);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3730 次 |
| 最近记录: |