{"id":1013,"date":"2017-03-30T17:22:04","date_gmt":"2017-03-30T09:22:04","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1013"},"modified":"2017-03-30T17:22:04","modified_gmt":"2017-03-30T09:22:04","slug":"android%e5%bc%80%e5%8f%91%e4%b9%8banimation%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/android\/2017\/03\/30\/1722041013.html","title":{"rendered":"Android\u5f00\u53d1\u4e4bAnimation\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<p>1\u3001Animation\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5e38\u7528\u7684\u76f8\u5173\u5b50\u7c7b\u6709\u4e94\u4e2a\uff1aAnimationSet\u3001AlphaAnimation\u3001RotateAnimation\u3001ScaleAnimation\u3001TranslateAnimation\uff1b<br \/>\n2\u3001AnimationSet\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u89c6\u56fe\u8bbe\u7f6e\u52a8\u753b\u65f6\u63a5\u6536\u7684\u662fAnimationSet\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4ed6\u53ef\u4ee5\u5c06\u591a\u4e2a\u52a8\u753b\u540c\u65f6\u5e94\u7528\u5230\u5355\u4e2a\u89c6\u56fe\u4e0a\uff1b<br \/>\n3\u3001AlphaAnimation\u8868\u793a\u7684\u6e10\u53d8\u52a8\u753b\uff0c\u4ece\u5168\u663e\u793a\u5230\u5168\u9690\u85cf\uff1b<br \/>\n4\u3001RotateAnimation\u662f\u65cb\u8f6c\u52a8\u753b\uff0c\u4ee5\u67d0\u4e00\u4e2a\u70b9\u4e3a\u5706\u5fc3\u8fdb\u884c\u65cb\u8f6c\uff1b<br \/>\n5\u3001ScaleAnimation\u662f\u7f29\u653e\u52a8\u753b\uff0c\u4ee5\u67d0\u4e00\u4e2a\u70b9\u4e3a\u7f29\u653e\u7ec8\u70b9\uff0c\u8fdb\u884c\u767e\u5206\u6bd4\u7f29\u653e\uff1b<br \/>\n6\u3001TranslateAnimation\u662f\u5e73\u79fb\u52a8\u753b\uff0c\u4ece\u5f53\u524d\u4f4d\u7f6e\u79fb\u52a8\u5230\u67d0\u4e2a\u5750\u6807\u4f4d\u7f6e\u52a8\u753b\uff1b<br \/>\n7\u3001Animation\u7684\u76f8\u5173\u65b9\u6cd5\u8fd8\u6709\uff1a\u52a8\u753b\u6267\u884c\u65f6\u95f4setDuration()\uff0c\u662f\u5426\u505c\u7559\u5728\u52a8\u753b\u7ed3\u675f\u4f4d\u7f6esetFillAfter()\uff0c\u91cd\u590d\u6b21\u6570setRepeatCount()\uff0c\u5ef6\u65f6\u6267\u884c\u65f6\u95f4setStartOffset()\u7b49\uff1b<br \/>\n8\u3001\u6807\u51c6\u7684\u52a8\u753b\u8bbe\u7f6e\u6b65\u9aa4\u6709\uff1a\u521b\u5efaAnimationSet\u3001Animation\u5bf9\u8c61&#8211;>\u8bbe\u7f6e\u57fa\u672c\u5c5e\u6027(\u6267\u884c\u65f6\u95f4\u7b49)&#8211;>Animation\u52a0\u5165\u5230AnimationSet&#8211;>\u89c6\u56fe\u6267\u884c\u52a8\u753b\uff1b<br \/>\n9\u3001\u5b9e\u73b04\u79cd\u52a8\u753b\u7684\u57fa\u672c\u529f\u80fd\uff0c\u521b\u5efa4\u4e2a\u6309\u94ae\uff0c\u4e00\u4e2a\u6309\u94ae\u5bf9\u5e94\u4e00\u79cd\u52a8\u753b\u7c7b\u578b\uff0c\u521b\u5efa\u4e00\u4e2aImageView\uff0c\u5c06\u6240\u6709\u52a8\u753b\u5728\u8be5\u89c6\u56fe\u4e0a\u4f53\u73b0\uff1b<\/p>\n<p>\u5173\u952e\u4ee3\u7801\uff1a<!--more--><\/p>\n<p>activity_main.xml<\/p>\n<pre line=\"1\" lang=\"java\"><?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"\n    android:weightSum=\"2\"\n    tools:context=\"com.yusian.animations.MainActivity\">\n\n    <ImageView\n        android:id=\"@+id\/img_main\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:src=\"@mipmap\/ic_launcher\"\n        android:layout_margin=\"50dp\"\n        android:layout_gravity=\"center\"\n        android:background=\"#CCC\"\/>\n\n    <LinearLayout\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:layout_weight=\"1\"\n        android:orientation=\"vertical\"\n        android:gravity=\"bottom\">\n        <Button\n            android:id=\"@+id\/btn_alpha\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"50dp\"\n            android:text=\"\u6e10\u53d8\"\/>\n        <Button\n            android:id=\"@+id\/btn_rotate\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"50dp\"\n            android:text=\"\u65cb\u8f6c\"\/>\n        <Button\n            android:id=\"@+id\/btn_scale\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"50dp\"\n            android:text=\"\u7f29\u653e\"\/>\n        <Button\n            android:id=\"@+id\/btn_translate\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"50dp\"\n            android:text=\"\u5e73\u79fb\"\/>\n\n    <\/LinearLayout>\n\n<\/LinearLayout><\/pre>\n<p>MainActivity<\/p>\n<pre line=\"1\" lang=\"java\">package com.yusian.animations;\n\nimport android.support.v7.app.AppCompatActivity;\nimport android.os.Bundle;\nimport android.view.View;\nimport android.view.animation.AlphaAnimation;\nimport android.view.animation.Animation;\nimport android.view.animation.AnimationSet;\nimport android.view.animation.RotateAnimation;\nimport android.view.animation.ScaleAnimation;\nimport android.view.animation.TranslateAnimation;\n\nimport android.widget.ImageView;\n\npublic class MainActivity extends AppCompatActivity implements View.OnClickListener{\n    private ImageView imageView;\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        imageView = (ImageView) findViewById(R.id.img_main);\n        findViewById(R.id.btn_alpha).setOnClickListener(this);\n        findViewById(R.id.btn_rotate).setOnClickListener(this);\n        findViewById(R.id.btn_scale).setOnClickListener(this);\n        findViewById(R.id.btn_translate).setOnClickListener(this);\n    }\n\n    @Override\n    public void onClick(View v) {\n        switch (v.getId()){\n            \/\/ \u6e10\u53d8\n            case R.id.btn_alpha:{\n                AnimationSet animationSet = new AnimationSet(true);\n                \/\/ \u521b\u5efa\u6e10\u53d8\u52a8\u753b\n                Animation alphaAnimation = new AlphaAnimation(1,0);\n                alphaAnimation.setDuration(1000);\n\n                \/\/ \u6dfb\u52a0\u5230AnimationSet\u53ca\u6267\u884c\u52a8\u753b\n                animationSet.addAnimation(alphaAnimation);\n                animationSet.setFillAfter(true);    \/\/ \u505c\u7559\u5728\u7ed3\u5c3e\u72b6\u6001\n                animationSet.setFillBefore(false);\n                animationSet.setRepeatCount(2);     \/\/ \u91cd\u590d\u6b21\u6570\n                animationSet.setStartOffset(1);     \/\/ \u5ef6\u65f6\u65f6\u95f4\n                imageView.startAnimation(animationSet);\n            }break;\n            \/\/ \u65cb\u8f6c\n            case R.id.btn_rotate:{\n                AnimationSet animationSet = new AnimationSet(true);\n\n                Animation rotateAnimation = new RotateAnimation(0,36000,\n                        Animation.RELATIVE_TO_SELF, 0.5f,\n                        Animation.RELATIVE_TO_SELF, 0.5f);\n                animationSet.setDuration(5000);\n\n                animationSet.addAnimation(rotateAnimation);\n                imageView.startAnimation(animationSet);\n            }break;\n            \/\/ \u7f29\u653e\n            case R.id.btn_scale:{\n                AnimationSet animationSet = new AnimationSet(true);\n\n                Animation animation = new ScaleAnimation(1,0,1,0,\n                        Animation.RELATIVE_TO_SELF, 0.5f,\n                        Animation.RELATIVE_TO_SELF, 0.5f);\n                animation.setDuration(1000);\n\n                animationSet.addAnimation(animation);\n                imageView.startAnimation(animationSet);\n            }break;\n            \/\/ \u5e73\u79fb\n            case R.id.btn_translate:{\n                AnimationSet animationSet = new AnimationSet(true);\n\n                Animation animation = new TranslateAnimation(\n                        Animation.RELATIVE_TO_SELF,0f,\n                        Animation.RELATIVE_TO_SELF,1f,\n                        Animation.RELATIVE_TO_SELF,0f,\n                        Animation.RELATIVE_TO_SELF,1f);\n                animation.setDuration(1000);\n\n                animationSet.addAnimation(animation);\n                imageView.startAnimation(animationSet);\n            }break;\n            default:break;\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001Animation\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5e38\u7528\u7684\u76f8\u5173\u5b50\u7c7b\u6709\u4e94\u4e2a\uff1aAnimationSet\u3001AlphaAnimation\u3001RotateAnimation\u3001ScaleAnimation\u3001TranslateAnimation\uff1b 2\u3001AnimationSet\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u89c6\u56fe\u8bbe\u7f6e\u52a8\u753b\u65f6\u63a5\u6536\u7684\u662fAnimationSet\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4ed6\u53ef\u4ee5\u5c06\u591a\u4e2a\u52a8\u753b\u540c\u65f6\u5e94\u7528\u5230\u5355\u4e2a\u89c6\u56fe\u4e0a\uff1b 3\u3001AlphaAnimation\u8868\u793a\u7684\u6e10\u53d8\u52a8\u753b\uff0c\u4ece\u5168\u663e\u793a\u5230\u5168\u9690\u85cf\uff1b 4\u3001RotateAnimation\u662f\u65cb\u8f6c\u52a8\u753b\uff0c\u4ee5\u67d0\u4e00\u4e2a\u70b9\u4e3a\u5706\u5fc3\u8fdb\u884c\u65cb\u8f6c\uff1b 5\u3001ScaleAnimation\u662f\u7f29\u653e\u52a8\u753b\uff0c\u4ee5\u67d0\u4e00\u4e2a\u70b9\u4e3a\u7f29\u653e\u7ec8\u70b9\uff0c\u8fdb\u884c\u767e\u5206\u6bd4\u7f29\u653e\uff1b 6\u3001TranslateAnimation\u662f\u5e73\u79fb\u52a8\u753b\uff0c\u4ece\u5f53\u524d\u4f4d\u7f6e\u79fb\u52a8\u5230\u67d0\u4e2a\u5750\u6807\u4f4d\u7f6e\u52a8\u753b\uff1b 7\u3001Animation\u7684\u76f8\u5173\u65b9\u6cd5\u8fd8\u6709\uff1a\u52a8\u753b\u6267\u884c\u65f6\u95f4setDuration()\uff0c\u662f\u5426\u505c\u7559\u5728\u52a8\u753b\u7ed3\u675f\u4f4d\u7f6esetFillAfter()\uff0c\u91cd\u590d\u6b21\u6570setRepeatCount()\uff0c\u5ef6\u65f6\u6267\u884c\u65f6\u95f4setStartOffset()\u7b49\uff1b 8\u3001\u6807\u51c6\u7684\u52a8\u753b\u8bbe\u7f6e\u6b65\u9aa4\u6709\uff1a\u521b\u5efaAnimationSet\u3001Animation\u5bf9\u8c61&#8211;>\u8bbe\u7f6e\u57fa\u672c\u5c5e\u6027(\u6267\u884c\u65f6\u95f4\u7b49)&#8211;>Animation\u52a0\u5165\u5230AnimationSet&#8211;>\u89c6\u56fe\u6267\u884c\u52a8\u753b\uff1b 9\u3001\u5b9e\u73b04\u79cd\u52a8\u753b\u7684\u57fa\u672c\u529f\u80fd\uff0c\u521b\u5efa4\u4e2a\u6309\u94ae\uff0c\u4e00\u4e2a\u6309\u94ae\u5bf9\u5e94\u4e00\u79cd\u52a8\u753b\u7c7b\u578b\uff0c\u521b\u5efa\u4e00\u4e2aImageView\uff0c\u5c06\u6240\u6709\u52a8\u753b\u5728\u8be5\u89c6\u56fe\u4e0a\u4f53\u73b0\uff1b \u5173\u952e\u4ee3\u7801\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[33],"class_list":["post-1013","post","type-post","status-publish","format-standard","hentry","category-android","tag-33"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1013"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1013\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}