{"id":945,"date":"2023-03-11T18:53:57","date_gmt":"2023-03-11T10:53:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=945"},"modified":"2023-04-29T15:40:23","modified_gmt":"2023-04-29T07:40:23","slug":"android-color-gradient-implementation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-color-gradient-implementation\/","title":{"rendered":"Android\u989c\u8272\u6e10\u53d8\uff08gradient\uff09\u5b9e\u73b0"},"content":{"rendered":"<h2>XML<\/h2>\n<p>shape_gradient.xml<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;shape xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;&gt;\n    &lt;gradient\n        android:angle=&quot;-90&quot;\n        android:startColor=&quot;@color\/colorPrimaryDark&quot;\n        android:endColor=&quot;@color\/colorPrimary&quot;\n        \/&gt;\n&lt;\/shape&gt;<\/code><\/pre>\n<h2>LinearGradient<\/h2>\n<pre><code class=\"language-java\">\/**\n @param x0           \u8d77\u70b9X\u5750\u6807\n @param y0           \u8d77\u70b9Y\u5750\u6807\n @param x1           \u7ec8\u70b9X\u5750\u6807\n @param y1           \u7ec8\u70b9Y\u5750\u6807\n @param  colors      \u6240\u6709\u989c\u8272\u6e10\u53d8\u96c6\u5408\n @param  positions   \u53ef\u4ee5\u8bbe\u7f6e\u5747\u5300\u6e10\u53d8\uff0c\u4e5f\u53ef\u4ee5\u6309\u7167\u81ea\u5b9a\u4e49\u6bd4\u4f8b\u8fdb\u884c\u6e10\u53d8\uff0c\u8fd8\u53ef\u4ee5\u4e3anull\u3002\u5047\u8bbe1\u4e3a\u6574\u4e2a\u6e10\u53d8\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u8bbe\u7f6e\u7684\u6240\u6709\u989c\u8272\uff08\u5047\u8bbe\u67094\u79cd\u989c\u8272\uff09\uff0c\u90fd\u4ee5\u540c\u7b49\u7684\u6743\u91cd\uff08\u6e10\u53d8\u957f\u5ea6\u6bd4\u4f8b0.25\uff1a0.25\uff1a0.25\uff1a0.25\uff09\u8fdb\u884c\u989c\u8272\u6e10\u53d8\u3002\n @param  tile        \u7740\u8272\u5668\u7684\u4e0d\u540c\u6a21\u5f0f\n*\/\npublic LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],\n           TileMode tile)<\/code><\/pre>\n<pre><code class=\"language-java\">public class MyView extends View {\n\n    public MyView(Context context) {\n        super(context);\n    }\n\n    public MyView(Context context, @Nullable AttributeSet attrs) {\n        super(context, attrs);\n    }\n\n    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n    }\n\n    @Override\n    protected void onDraw(Canvas canvas) {\n        super.onDraw(canvas);\n        \/\/\u83b7\u53d6View\u7684\u5bbd\u9ad8\n        int width = getWidth();\n        int height = getHeight();\n\n        int colorStart = getResources().getColor(R.color.colorStart);\n        int colorCenter = getResources().getColor(R.color.colorCenter);\n        int colorEnd = getResources().getColor(R.color.colorEnd);\n\n        Paint paint = new Paint();\n        LinearGradient backGradient = new LinearGradient(0, 0, 0, height, new int[]{colorStart, colorCenter ,colorEnd}, null, Shader.TileMode.CLAMP);\n        paint.setShader(backGradient);\n        canvas.drawRect(0, 0, width, height, paint);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>XML shape_gradient.xml &lt;?xml version=&quot;1.0&quot; [&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-945","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\/945","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=945"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/945\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}