android怎么用paint实现图像的渐变出现

在android.graphics中提供了有关Gradient字样的类,例如LinearGradient线性渐变、 RadialGradient径向渐变和SweepGradient角度渐变三种,他们的基类为android.graphics.Shader。为了演 示图像渐变效果,下面给出一个简单的实例。

  一、LinearGradient线性渐变
  在android平台中提供了两种重载方式来实例化该类分别为,他们的不同之处为参数中第一种方法可以用颜色数组,和位置来实现更细腻的过渡效果, 比如颜 色采样int[] colors数组中存放20种颜色,则渐变将会逐一处理。而第二种方法参数仅为起初颜色color0和最终颜色color1。

  LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile)

  LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)

  使用实例如下:

  Paint p=new Paint();
  LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);  //参数一为渐变起初点坐标x位置,参数二为y轴位置,参数三和四分辨对应渐变终点,最后参数为平铺方式,这里设置为镜像
  刚才已经讲到Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变,代码如下:

  p.setShader(lg);
  canvas.drawCicle(0,0,200,p); //参数3为画圆的半径,类型为float型。
  二、RadialGradient镜像渐变
  有了上面的基础,我们一起来了解下径向渐变。和上面参数唯一不同的是,径向渐变第三个参数是半径,其他的和线性渐变相同。

  RadialGradient(float x, float y, float radius, int[] colors, float[] positions, Shader.TileMode tile)

  RadialGradient(float x, float y, float radius, int color0, int color1, Shader.TileMode tile)

  三、SweepGradient角度渐变
  对于一些3D立体效果的渐变可以尝试用角度渐变来完成一个圆锥形,相对来说比上面更简单,前两个参数为中心点,然后通过载入的颜色来平均的渐变渲染。

  SweepGradient(float cx, float cy, int[] colors, float[] positions)  //对于最后一个参数SDK上的描述为May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.,所以建议使用下面的重载方法,本方法一般为NULL即可。

  SweepGradient(float cx, float cy, int color0, int color1)

  到此,希望大家对图像特效处理有了一定的认识,了解这些对打好Android游戏开发的基础很有好处。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中的 Paint 类可以用来绘制各种形状和线条。下面是一个简单的示例,演示如何使用 Paint 类在 Android 中绘制直线。 首先,在 XML 布局文件中添加一个 View: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:id="@+id/drawing_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> ``` 然后,在 Activity 中获取该 View 并绘制直线: ```java public class MainActivity extends AppCompatActivity { private View drawingView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawingView = findViewById(R.id.drawing_view); drawingView.setBackground(new CustomView(this)); } private class CustomView extends View { private Paint paint; public CustomView(Context context) { super(context); init(); } private void init() { paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(5f); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int startX = 0; int startY = 0; int stopX = getWidth(); int stopY = getHeight(); canvas.drawLine(startX, startY, stopX, stopY, paint); } } } ``` 在这个示例中,我们创建了一个 CustomView 类,继承自 View 类,并在该类中实现了 onDraw() 方法。onDraw() 方法用于绘制 View 上的内容,我们在这里使用 Canvas 类的 drawLine() 方法绘制了一条直线。 Paint 类的实例被创建在 CustomView 类的构造函数中,我们使用该实例设置了直线的颜色和线宽。如果需要绘制其他形状,也可以使用 Paint 类的其他方法来设置笔刷、阴影、渐变等效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值