{"id":958,"date":"2023-03-11T19:36:11","date_gmt":"2023-03-11T11:36:11","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=958"},"modified":"2023-04-29T13:30:06","modified_gmt":"2023-04-29T05:30:06","slug":"android-objectanimator-record","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-objectanimator-record\/","title":{"rendered":"Android ObjectAnimator\u8bb0\u5f55"},"content":{"rendered":"<p>ObjectAnimator\u7ee7\u627f\u81eaValueAnimator\uff0c\u6240\u4ee5ValueAnimator\u6240\u80fd\u4f7f\u7528\u7684\u65b9\u6cd5\uff0cObjectAnimator\u90fd\u53ef\u4ee5\u4f7f\u7528\uff0cObjectAnimator\u540c\u65f6\u4e5f\u91cd\u5199\u4e86\u51e0\u4e2a\u65b9\u6cd5\uff0c\u6bd4\u5982\uff1a<code>ofInt()<\/code>\u3001<code>ofFloat()<\/code>\u7b49\u3002<\/p>\n<h2>\u57fa\u672c\u4f7f\u7528<\/h2>\n<pre><code class=\"language-java\">public static ObjectAnimator ofFloat(Object target, String propertyName, float... values)<\/code><\/pre>\n<p><!-- more --><\/p>\n<ul>\n<li>target\uff1a\u6307\u5b9a\u6267\u884c\u52a8\u753b\u7684\u63a7\u4ef6<\/li>\n<li>propertyName\uff1a\u6307\u5b9a\u63a7\u4ef6\u7684\u5c5e\u6027<\/li>\n<li>values\uff1a\u53ef\u53d8\u957f\u53c2\u6570<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/\u900f\u660e\u5ea6\u52a8\u753b\nObjectAnimator animator = ObjectAnimator.ofFloat(view, &quot;alpha&quot;, 1, 0, 1);\nanimator.setDuration(2000);\nanimator.start();\n\n\/\/\u65cb\u8f6c\u52a8\u753b\uff1a\u56f4\u7ed5x\u8f74\u65cb\u8f6c\nObjectAnimator animator = ObjectAnimator.ofFloat(tv, &quot;rotationX&quot;, 0, 270, 0);\nanimator.setDuration(2000);\nanimator.start();\n\n\/\/\u65cb\u8f6c\u52a8\u753b\uff1a\u56f4\u7ed5y\u8f74\u65cb\u8f6c\nObjectAnimator animator = ObjectAnimator.ofFloat(tv, &quot;rotationY&quot;, 0, 180, 0);\nanimator.setDuration(2000);\nanimator.start();\n\n\/\/\u65cb\u8f6c\u52a8\u753b\uff1a\u56f4\u7ed5z\u8f74\u65cb\u8f6c\nObjectAnimator animator = ObjectAnimator.ofFloat(tv, &quot;rotation&quot;, 0, 270, 0);\nanimator.setDuration(2000);\nanimator.start();\n\n\/\/\u5e73\u79fb\u52a8\u753b\uff1a\u5728x\u8f74\u4e0a\u5e73\u79fb\nObjectAnimator animator = ObjectAnimator.ofFloat(tv, &quot;translationX&quot;, 0, 200, -200, 0);\nanimator.setDuration(2000);\nanimator.start();\n\n\/\/\u5e73\u79fb\u52a8\u753b\uff1a\u5728y\u8f74\u4e0a\u5e73\u79fb\nObjectAnimator animator = ObjectAnimator.ofFloat(tv, &quot;translationY&quot;, 0, 200, -100, 0);\nanimator.setDuration(2000);\nanimator.start();\n\n\/\/\u7f29\u653e\u52a8\u753b\uff1a\u5728x\u8f74\u4e0a\u7f29\u653e\nObjectAnimator animator = ObjectAnimator.ofFloat(tv, &quot;scaleX&quot;, 0, 3, 1);\nanimator.setDuration(2000);\nanimator.start();\n\n\/\/\u7f29\u653e\u52a8\u753b\uff1a\u5728y\u8f74\u4e0a\u7f29\u653e\nObjectAnimator animator = ObjectAnimator.ofFloat(tv, &quot;scaleY&quot;, 0, 3, 1);\nanimator.setDuration(2000);\nanimator.start();<\/code><\/pre>\n<h2>ObjectAnimator\u52a8\u753b\u539f\u7406<\/h2>\n<p>ObjectAnimator\u52a8\u753b\u539f\u7406\uff1a\u6839\u636e\u5c5e\u6027\u503c\u62fc\u88c5\u6210\u5bf9\u5e94\u7684set\u51fd\u6570\u7684\u540d\u5b57\uff0c\u6bd4\u5982<code>scaleX<\/code>\u7684\u62fc\u63a5\u65b9\u6cd5\u5c31\u662f\u5c06\u5c5e\u6027\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u5f3a\u5236\u5927\u5199\u540e\uff0c\u4e0eset\u62fc\u63a5\uff0c\u4e5f\u5c31\u662f<code>setScaleX<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u53cd\u5c04\u627e\u5230\u5bf9\u5e94\u63a7\u4ef6\u7684<code>setScaleX(float scaleX)<\/code>\u51fd\u6570\uff0c\u5c06\u5f53\u524d\u6570\u5b57\u503c\u4f5c\u4e3a<code>setScaleX(float scale)\u7684\u53c2\u6570\u5c06\u5176\u4f20\u5165\u3002\u5c5e\u6027\u503c\u7684\u9996\u5b57\u6bcd\u5927\u5c0f\u5199\u90fd\u53ef\u4ee5\uff0c\u6700\u7ec8\u90fd\u4f1a\u88ab\u5f3a\u8f6c\u6210\u5927\u5199\u3002View\u4e2d\u90fd\u5df2\u7ecf\u5b9e\u73b0\u4e86<\/code>alpha<code>\u3001<\/code>rotation<code>\u3001<\/code>translate<code>\u3001<\/code>scale`\u76f8\u5173\u7684set\u65b9\u6cd5\u3002<\/p>\n<h2>\u81ea\u5b9a\u4e49ObjectAnimator\u5c5e\u6027<\/h2>\n<pre><code class=\"language-java\">ObjectAnimator objectAnimator = ObjectAnimator.ofInt(mCircleView,&quot;pointRadius&quot;, 0, 200, 100, 200, 50);\nobjectAnimator.setDuration(1000);\nobjectAnimator.start();<\/code><\/pre>\n<p>\u81ea\u5b9a\u4e49Point<\/p>\n<pre><code class=\"language-java\">public class Ponit {\n    private int radius;\n\n    public Ponit() {\n    }\n\n    public Ponit(int radius) {\n        this.radius = radius;\n    }\n\n    public int getRadius() {\n        return radius;\n    }\n\n    public void setRadius(int radius) {\n        this.radius = radius;\n    }\n}<\/code><\/pre>\n<p>\u81ea\u5b9a\u4e49view<\/p>\n<pre><code class=\"language-java\">public class CircleView extends View {\n    private Ponit mPoint = new Ponit();\n    private Paint mPiant;\n    private int mScreenWidth; \/\/\u5c4f\u5e55\u5bbd\u5ea6\n\n    public CircleView(Context context) {\n        this(context, null);\n    }\n\n    public CircleView(Context context, @Nullable AttributeSet attrs) {\n        this(context, attrs, 0);\n    }\n\n    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n        mPiant = new Paint(Paint.ANTI_ALIAS_FLAG);\n        mPiant.setColor(Color.RED);\n        mPiant.setStyle(Paint.Style.FILL);\n        mScreenWidth = ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();\n    }\n\n    @Override\n    protected void onDraw(Canvas canvas) {\n        if (mPoint != null) {\n            canvas.drawCircle(mScreenWidth\/2, getY()+getPaddingTop(), mPoint.getRadius(), mPiant);\n        }\n    }\n\n    public int getPointRadius() {\n        return mPoint.getRadius();\n    }\n\n    public void setPointRadius(int radius) {  \/\/set\u65b9\u6cd5\u5fc5\u987b\u548cObjectAnimator\u4e2d\u7684\u5c5e\u6027\u503c\u5bf9\u5e94\n        mPoint.setRadius(radius);\n        invalidate();\n    }\n}<\/code><\/pre>\n<h2>ObjectAnimator\u6539\u53d8\u63a7\u4ef6\u80cc\u666f\u989c\u8272<\/h2>\n<p>\u4ee5TextView\u4e3a\u7c7b\uff0c\u6539\u53d8\u5176\u80cc\u666f\u989c\u8272\uff0c\u5173\u952e\u65b9\u6cd5\u662f\u7ee7\u627fView\u7684\u63a7\u4ef6\u90fd\u5b9e\u73b0\u7684\u8fd9\u4e2a\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public void setBackgroundColor(int color);<\/code><\/pre>\n<pre><code class=\"language-java\">ObjectAnimator objectAnimator = ObjectAnimator.ofInt(tv, &quot;backgroundColor&quot;, 0xfff10f0f, 0xff0f94f1, 0xffeaf804, 0xfff92a0f);\nobjectAnimator.setDuration(2000);\nobjectAnimator.setEvaluator(new ArgbEvaluator());\nobjectAnimator.start();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ObjectAnimator\u7ee7\u627f\u81eaValueAnimator\uff0c\u6240\u4ee5ValueAnimator\u6240\u80fd\u4f7f\u7528\u7684\u65b9\u6cd5\uff0cO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-958","post","type-post","status-publish","format-standard","hentry","category-android-ui"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=958"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/958\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}