使用GradientDrawable

本文提供了一个Android应用程序示例,展示了如何使用GradientDrawable类来创建带有渐变效果的矩形,并通过不同类型的渐变(线性、径向、扫掠)及角落圆角设置来丰富UI设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
002 * Copyright (C) 2007 The Android Open Source Project
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 
017package app.test;
018 
019import android.app.Activity;
020import android.content.Context;
021import android.graphics.Canvas;
022import android.graphics.Paint;
023import android.graphics.Path;
024import android.graphics.Picture;
025import android.graphics.Rect;
026import android.graphics.drawable.Drawable;
027import android.graphics.drawable.GradientDrawable;
028import android.os.Bundle;
029import android.util.AttributeSet;
030import android.view.View;
031import android.view.ViewGroup;
032import android.view.ViewParent;
033 
034public class Test extends GraphicsActivity {
035 
036  @Override
037  protected void onCreate(Bundle savedInstanceState) {
038    super.onCreate(savedInstanceState);
039    setContentView(new SampleView(this));
040  }
041 
042  private static class SampleView extends View {
043    private Path mPath;
044    private Paint mPaint;
045    private Rect mRect;
046    private GradientDrawable mDrawable;
047 
048    public SampleView(Context context) {
049      super(context);
050      setFocusable(true);
051 
052      mPath = new Path();
053      mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
054      mRect = new Rect(0, 0, 120, 120);
055 
056      mDrawable = new GradientDrawable(
057          GradientDrawable.Orientation.TL_BR, new int[] { 0xFFFF0000,
058              0xFF00FF00, 0xFF0000FF });
059      mDrawable.setShape(GradientDrawable.RECTANGLE);
060      mDrawable.setGradientRadius((float) (Math.sqrt(2) * 60));
061    }
062 
063    static void setCornerRadii(GradientDrawable drawable, float r0,
064        float r1, float r2, float r3) {
065      drawable.setCornerRadii(new float[] { r0, r0, r1, r1, r2, r2, r3,
066          r3 });
067    }
068 
069    @Override
070    protected void onDraw(Canvas canvas) {
071 
072      mDrawable.setBounds(mRect);
073 
074      float r = 16;
075 
076      canvas.save();
077      canvas.translate(10, 10);
078      mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
079      setCornerRadii(mDrawable, r, r, 0, 0);
080      mDrawable.draw(canvas);
081      canvas.restore();
082 
083      canvas.save();
084      canvas.translate(10 + mRect.width() + 10, 10);
085      mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
086      setCornerRadii(mDrawable, 0, 0, r, r);
087      mDrawable.draw(canvas);
088      canvas.restore();
089 
090      canvas.translate(0, mRect.height() + 10);
091 
092      canvas.save();
093      canvas.translate(10, 10);
094      mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);
095      setCornerRadii(mDrawable, 0, r, r, 0);
096      mDrawable.draw(canvas);
097      canvas.restore();
098 
099      canvas.save();
100      canvas.translate(10 + mRect.width() + 10, 10);
101      mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
102      setCornerRadii(mDrawable, r, 0, 0, r);
103      mDrawable.draw(canvas);
104      canvas.restore();
105 
106      canvas.translate(0, mRect.height() + 10);
107 
108      canvas.save();
109      canvas.translate(10, 10);
110      mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
111      setCornerRadii(mDrawable, r, 0, r, 0);
112      mDrawable.draw(canvas);
113      canvas.restore();
114 
115      canvas.save();
116      canvas.translate(10 + mRect.width() + 10, 10);
117      mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);
118      setCornerRadii(mDrawable, 0, r, 0, r);
119      mDrawable.draw(canvas);
120      canvas.restore();
121    }
122  }
123}
124 
125class GraphicsActivity extends Activity {
126  // set to true to test Picture
127  private static final boolean TEST_PICTURE = false;
128 
129  @Override
130  protected void onCreate(Bundle savedInstanceState) {
131    super.onCreate(savedInstanceState);
132  }
133 
134  @Override
135  public void setContentView(View view) {
136    if (TEST_PICTURE) {
137      ViewGroup vg = new PictureLayout(this);
138      vg.addView(view);
139      view = vg;
140    }
141 
142    super.setContentView(view);
143  }
144}
145 
146class PictureLayout extends ViewGroup {
147  private final Picture mPicture = new Picture();
148 
149  public PictureLayout(Context context) {
150    super(context);
151  }
152 
153  public PictureLayout(Context context, AttributeSet attrs) {
154    super(context, attrs);
155  }
156 
157  @Override
158  public void addView(View child) {
159    if (getChildCount() > 1) {
160      throw new IllegalStateException(
161          "PictureLayout can host only one direct child");
162    }
163 
164    super.addView(child);
165  }
166 
167  @Override
168  public void addView(View child, int index) {
169    if (getChildCount() > 1) {
170      throw new IllegalStateException(
171          "PictureLayout can host only one direct child");
172    }
173 
174    super.addView(child, index);
175  }
176 
177  @Override
178  public void addView(View child, LayoutParams params) {
179    if (getChildCount() > 1) {
180      throw new IllegalStateException(
181          "PictureLayout can host only one direct child");
182    }
183 
184    super.addView(child, params);
185  }
186 
187  @Override
188  public void addView(View child, int index, LayoutParams params) {
189    if (getChildCount() > 1) {
190      throw new IllegalStateException(
191          "PictureLayout can host only one direct child");
192    }
193 
194    super.addView(child, index, params);
195  }
196 
197  @Override
198  protected LayoutParams generateDefaultLayoutParams() {
199    return new LayoutParams(LayoutParams.MATCH_PARENT,
200        LayoutParams.MATCH_PARENT);
201  }
202 
203  @Override
204  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
205    final int count = getChildCount();
206 
207    int maxHeight = 0;
208    int maxWidth = 0;
209 
210    for (int i = 0; i < count; i++) {
211      final View child = getChildAt(i);
212      if (child.getVisibility() != GONE) {
213        measureChild(child, widthMeasureSpec, heightMeasureSpec);
214      }
215    }
216 
217    maxWidth += getPaddingLeft() + getPaddingRight();
218    maxHeight += getPaddingTop() + getPaddingBottom();
219 
220    Drawable drawable = getBackground();
221    if (drawable != null) {
222      maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
223      maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
224    }
225 
226    setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
227        resolveSize(maxHeight, heightMeasureSpec));
228  }
229 
230  private void drawPict(Canvas canvas, int x, int y, int w, int h, float sx,
231      float sy) {
232    canvas.save();
233    canvas.translate(x, y);
234    canvas.clipRect(0, 0, w, h);
235    canvas.scale(0.5f, 0.5f);
236    canvas.scale(sx, sy, w, h);
237    canvas.drawPicture(mPicture);
238    canvas.restore();
239  }
240 
241  @Override
242  protected void dispatchDraw(Canvas canvas) {
243    super.dispatchDraw(mPicture.beginRecording(getWidth(), getHeight()));
244    mPicture.endRecording();
245 
246    int x = getWidth() / 2;
247    int y = getHeight() / 2;
248 
249    if (false) {
250      canvas.drawPicture(mPicture);
251    } else {
252      drawPict(canvas, 0, 0, x, y, 1, 1);
253      drawPict(canvas, x, 0, x, y, -1, 1);
254      drawPict(canvas, 0, y, x, y, 1, -1);
255      drawPict(canvas, x, y, x, y, -1, -1);
256    }
257  }
258 
259  @Override
260  public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
261    location[0] = getLeft();
262    location[1] = getTop();
263    dirty.set(0, 0, getWidth(), getHeight());
264    return getParent();
265  }
266 
267  @Override
268  protected void onLayout(boolean changed, int l, int t, int r, int b) {
269    final int count = super.getChildCount();
270 
271    for (int i = 0; i < count; i++) {
272      final View child = getChildAt(i);
273      if (child.getVisibility() != GONE) {
274        final int childLeft = getPaddingLeft();
275        final int childTop = getPaddingTop();
276        child.layout(childLeft, childTop,
277            childLeft + child.getMeasuredWidth(),
278            childTop + child.getMeasuredHeight());
279 
280      }
281    }
282  }
283}
284 
285    
286     
287   
288//该安卓代码来自于http://www.8jiaban.com/view/a9984631
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值