我想为我的项目制作一个星形.
我搜索了一下,现在我可以通过使用画布使星星最终成形.
但是接下来我想要的对我来说真的很困难.真是一场噩梦.
我尝试并搜索,但没有任何效果.
我要的是星星必须由没有背景色的线条组成.
只有线..跨越线..
然后表示一条线并使其淡出,创建下一线并再次使其淡出,然后创建该线,然后再次进行淡入,我希望该动作永久存在.直到我将此活动的屏幕移到另一个屏幕.
解决方法:
这段代码取自我一段时间前从事的项目.我认为它可以满足您的需求.
首先,我不知道您如何为星星创建线条.我只是使用矢量可绘制对象来创建星形线(有关如何使用VectorDrawables的更多详细信息,请参见this帖子).我应该警告你,我赶时间,所以不会很漂亮
警告:这篇文章会很长
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>
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>
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>
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>
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>
好.现在您已经拥有了所有必需的可绘制对象,让我们跳到Activity XML并创建布局.只是这里的基本内容.我将使用5个ImageVIEw来容纳不同的星形线(这使动画制作起来很方便,但可能会导致性能问题,我们将在后面讨论).我将使用ConstraintLayout作为根视图.
activity_test.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>
您可能已经注意到,我没有使用androID:src属性将任何可绘制对象分配给ImageVIEw.我鼓励您继续尝试.同时,我正在以编程方式添加图像.
在onCreate方法上方,声明以下变量
private ImageVIEw image1, image2, image3, image4, image5;private Context context;private int i = 1;private long duration = 800;private Handler animHandler;
animHandler将负责保持动画运行.我将使用i来跟踪要动画的ImageVIEw.顾名思义,场持续时间将保留动画所需的持续时间.
这是其余的代码.我将尽可能提供评论.
@OverrIDeprotected 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);}
setupVIEw
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));}
setVectorDrawable
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);}
在这里,我正在创建5个不同的位图来容纳5条线.到目前为止,这不会成为太大的问题.但是对于较大的项目(我必须一起使用69个位图),在内存中有很多位图可能不是一个好习惯. 5个位图应该可以正常工作.
startAnim
private voID startAnim() { runnable.run();}
可运行的
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());}
简而言之,我正在创建一个可运行的对象,该对象将根据我拥有的值变量对不同的ImageVIEw进行不同的动画处理.
让我再次强调,我已经从一个我曾经工作过的项目中发布了这段代码.即使它不能直接满足您的要求,我还是鼓励您尝试一下并尝试一下以使其正常工作.
编辑
该动画通过确保可重复运行来起作用.停止动画使用
animHandler.removeCallbacks(runnable);
总结 以上是内存溢出为你收集整理的如何在Android中制作星形并为其设置动画?全部内容,希望文章能够帮你解决如何在Android中制作星形并为其设置动画?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)