导航:首页 > 操作系统 > androidgradient渐变

androidgradient渐变

发布时间:2022-10-06 09:53:34

‘壹’ ios 怎么做到安卓的lineargradient效果

android 使用LinearGradient进行字体渐变的效果,如下图显示:

就像上面的显示效果一样一束白光闪过,这种效果主要还是使用了LinearGradient类来进行的

LinearGradient也称作线性渲染,LinearGradient的作用是实现某一区域内颜色的线性渐变效果

它有两个构造函数
代码如下 复制代码
public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)

其中,参数x0表示渐变的起始点x坐标;参数y0表示渐变的起始点y坐标;参数x1表示渐变的终点x坐标;参数y1表示渐变的终点y坐标;color0表示渐变开始颜色;color1表示渐变结束颜色;参数tile表示平铺方式。

Shader.TileMode有3种参数可供选择,分别为CLAMP、REPEAT和MIRROR:

CLAMP的作用是如果渲染器超出原始边界范围,则会复制边缘颜色对超出范围的区域进行着色

REPEAT的作用是在横向和纵向上以平铺的形式重复渲染位图

MIRROR的作用是在横向和纵向上以镜像的方式重复渲染位图

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

其中,参数x0表示渐变的起始点x坐标;参数y0表示渐变的起始点y坐标;参数x1表示渐变的终点x坐标;参数y1表示渐变的终点y坐标;参数colors表示渐变的颜色数组;参数positions用来指定颜色数组的相对位置;参数tile表示平铺方式。通常,参数positions设为null,表示颜色数组以斜坡线的形式均匀分布。

下面这段代码是直接从git上面的项目拷贝下来的
代码如下 复制代码

package com.example.shimmer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

private LinearGradient mLinearGradient;
private Matrix mGradientMatrix;
private Paint mPaint;
private int mViewWidth = 0;
private int mTranslate = 0;

private boolean mAnimating = true;

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0) {
mViewWidth = getMeasuredWidth();
if (mViewWidth > 0) {
mPaint = getPaint();
mLinearGradient = new LinearGradient(-mViewWidth, 0, 0, 0,
new int[] { 0x33ffffff, 0xffffffff, 0x33ffffff },
new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP);
mPaint.setShader(mLinearGradient);
mGradientMatrix = new Matrix();
}
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mAnimating && mGradientMatrix != null) {
mTranslate += mViewWidth / 10;
if (mTranslate > 2 * mViewWidth) {
mTranslate = -mViewWidth;
}
mGradientMatrix.setTranslate(mTranslate, 0);
mLinearGradient.setLocalMatrix(mGradientMatrix);
postInvalidateDelayed(50);
}
}

}

这段代码主要是分两步:一个是在onSizeChanged()即大小发生改变的时候,另外一个是onDraw()主要是用来做动画的效果的,

‘贰’ android 怎么自定义颜色渐变

1.在res/drawable/里新建XML文件(background_color.xml)
内容:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#000000"
android:endColor="#FFFFFF"
android:angle="90"
/>

</shape>
备:angle(角度)的值只可为:45 90 135 180等45的倍数

2.在res/layout里使用时:
颜色设置段:Android:src="@drawable/background_color

‘叁’ android gradient与solid是不是只能同时一个生效

android颜色渐变的分类有: LinearGradient线性渐变 RadialGradient镜像渐变 SweepGradient角度渐变 一、LinearGradient线性渐变顾名思义,是只颜色在一个直线方向上

‘肆’ android 如何实现背景图片渐变切换

解决方案1:
其他的和线性渐变相同。为了演 示图像渐变效果。

一, float radius, float x1。

SweepGradient(float cx。

LinearGradient(float x0,所以建议使用下面的重载方法,他们的基类为android、RadialGradient镜像渐变
有了上面的基础. If positions is NULL,代码如下,他们的不同之处为参数中第一种方法可以用颜色数组, int color1, float radius;/,然后通过载入的颜色来平均的渐变渲染, the drawing may proce unexpected results, int[] colors.RED,p).TileMode tile)

使用实例如下.TileMode, float[] positions; /, int color0,最后参数为平铺方式, Shader,100, float y0. If the values are not monotonic.graphics, float y1,这里设置为镜像
刚才已经讲到Gradient是基于Shader类,前两个参数为中心点.TileMode tile)

LinearGradient(float x0.drawCicle(0;
canvas.Shader,参数三和四分辨对应渐变终点.TileMode tile)

三, int color0,所以我们通过Paint的setShader方法来设置这个渐变,200,则渐变将会逐一处理。
二, float[] positions,径向渐变第三个参数是半径, int[] colors在android, beginning with 0 and ending with 1,本方法一般为NULL即可.、SweepGradient角度渐变
对于一些3D立体效果的渐变可以尝试用角度渐变来完成一个圆锥形, float cy. The relative position of each corresponding color in the colors array.setShader(lg),和位置来实现更细腻的过渡效果, 比如颜 色采样int[] colors数组中存放20种颜色, float x1,Shader。

RadialGradient(float x, float[] positions) /。而第二种方法参数仅为起初颜色color0和最终颜色color1, Shader。和上面参数唯一不同的是, then the colors are automatically spaced evenly,0, float y,了解这些对打好Android游戏开发的基础很有好处;对于最后一个参数SDK上的描述为May be NULL.graphics中提供了有关Gradient字样的类, Shader。

SweepGradient(float cx;参数3为画圆的半径;
LinearGradient lg=new LinearGradient(0,希望大家对图像特效处理有了一定的认识,0,参数二为y轴位置,我们一起来了解下径向渐变;/,类型为float型,相对来说比上面更简单,Color; /, int color1)

到此,100,Color.TileMode tile)

RadialGradient(float x.MIRROR).BLUE, float y0, int color0,例如LinearGradient线性渐变:

p, int color1、LinearGradient线性渐变
在android平台中提供了两种重载方式来实例化该类分别为,下面给出一个简单的实例, int[] colors, float cy, Shader, float y1;参数一为渐变起初点坐标x位置:

Paint p=new Paint(), float y.0、 RadialGradient径向渐变和SweepGradient角度渐变三种。

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

在android.graphics中提供了有关Gradient字样的类,例如LinearGradient线性渐变、RadialGradient径向渐变和SweepGradient角度渐变三种,他们的基类为android.graphics.Shader。为了演示图像渐变效果,下面给出一个简单的实例。一、LinearGradient线性渐变在android平台中提供了两种重载方式来实例化该类分别为,他们的不同之处为参数中第一种方法可以用颜色数组,和位置来实现更细腻的过渡效果,比如颜色采样int[]colors数组中存放20种颜色,则渐变将会逐一处理。而第二种方法参数仅为起初颜色color0和最终颜色color1。LinearGradient(floatx0,floaty0,floatx1,floaty1,int[]colors,float[]positions,Shader.TileModetile)LinearGradient(floatx0,floaty0,floatx1,floaty1,intcolor0,intcolor1,Shader.TileModetile)使用实例如下:Paintp=newPaint();LinearGradientlg=newLinearGradient(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(floatx,floaty,floatradius,int[]colors,float[]positions,Shader.TileModetile)RadialGradient(floatx,floaty,floatradius,intcolor0,intcolor1,Shader.TileModetile)三、SweepGradient角度渐变对于一些3D立体效果的渐变可以尝试用角度渐变来完成一个圆锥形,相对来说比上面更简单,前两个参数为中心点,然后通过载入的颜色来平均的渐变渲染。SweepGradient(floatcx,floatcy,int[]colors,float[]positions)//对于最后一个参数SDK上的描述为MaybeNULL.,beginningwith0andendingwith1.0.Ifthevaluesarenotmonotonic,.IfpositionsisNULL,.,所以建议使用下面的重载方法,本方法一般为NULL即可。SweepGradient(floatcx,floatcy,intcolor0,intcolor1)到此,希望大家对图像特效处理有了一定的认识,了解这些对打好Android游戏开发的基础很有好处。转载

‘陆’ android 颜色过渡

Android平台下实现渐变效果。在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轴位置,参数三和四分辨对应渐变终点,最后参数为平铺方式,这里设置为镜像.
刚才Android开发网已经讲到Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变,代码

‘柒’ 怎么在android上面做出根据形状来渐变的效果

<shape> Android:shape=["rectangle" | "oval" | "line" | "ring"] 其中rectagle矩形,oval椭圆,line水平直线,ring环形
<shape>中子节点的常用属性:
<gradient> 渐变
Android:startColor 起始颜色
Android:endColor 结束颜色
Android:angle 渐变角度,0从左到右,90表示从下到上,数值为45的整数倍,默认为0;
Android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep <solid > 填充
Android:color 填充的颜色
<stroke >描边
Android:width 描边的宽度
Android:color 描边的颜色
Android:dashWidth 表示'-'横线的宽度
Android:dashGap 表示'-'横线之间的距离
<corners >圆角
Android:radius 圆角的半径 值越大角越圆
Android:topRightRadius 右上圆角半径
Android:bottomLeftRadius 右下圆角角半径
Android:topLeftRadius 左上圆角半径
Android:bottomRightRadius 左下圆角半径
<padding >填充
android:bottom="1.0dip" 底部填充
android:left="1.0dip" 左边填充
android:right="1.0dip" 右边填充
android:top="0.0dip" 上面填充
Selector
根据不同的选定状态来定义不同的现实效果 分为四大属性:
android:state_selected 是选中
android:state_focused 是获得焦点
android:state_pressed 是点击
android:state_enabled 是设置是否响应事件,指所有事件
android:state_window_focused 默认时的背景图片 引用位置:res/drawable/文件的名称.xml
使用的方法:
Java代码中:R.drawable.文件的名称 XML中:Android:background="@drawable/文件的名称"
示例:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:Android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片-->
<item Android:drawable="@drawable/pic1" />
<!-- 没有焦点时的背景图片 -->
<item
Android:state_window_focused="false"
android:drawable="@drawable/pic_blue"
/>
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item
Android:state_focused="true"
android:state_pressed="true"
android:drawable= "@drawable/pic_red"
/>
<!-- 触摸模式下单击时的背景图片-->
<item
Android:state_focused="false"
Android:state_pressed="true"
Android:drawable="@drawable/pic_pink"
/>
<!--选中时的图片背景-->
<item
Android:state_selected="true"
android:drawable="@drawable/pic_orange"
/>
<!--获得焦点时的图片背景-->
<item
Android:state_focused="true"
Android:drawable="@drawable/pic_green"
/>
</selector>

layer-list(多个shape)
将多个图片或上面两种效果按照顺序层叠起来
示例:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" android:left="20dp">
<bitmap android:src="@drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>

感觉很像多个drawable
三者可以结合使用
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:bottom="8.0dip">
<shape>
<solid android:color="#ffaaaaaa" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="#ffaaaaaa" />

<padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="@color/setting_item_bgcolor_press" />
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list>
<item android:bottom="8.0dip">
<shape>
<solid android:color="#ffaaaaaa" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="#ffaaaaaa" />

<padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="@color/setting_item_bgcolor" />
</shape>
</item>
</layer-list>
</item>
</selector>

‘捌’ android shape怎么用

下图是安卓无忧中的例子,可以看里面的源码还有文档,网络一下安卓无忧,然后下载就行,大部分形状都可以定义,请看截图:

在Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的形状,首先我们了解一下

Shape下面有哪些标签,都代表什么意思:

1.1 solid:填充

android:color指定填充的颜色

1.2 gradient:渐变

android:startColor和android:endColor分别为起始和结束颜色,

android:angle是渐变角度,必须为45的整数倍。

另外渐变默认的模式为android:type="linear",即线性渐变,

可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。

angle值对应的位置如图:

1.3 stroke:描边

android:width="2dp" 描边的宽度,android:color 描边的颜色。

我们还可以把描边弄成虚线的形式,设置方式为:

android:dashWidth="5dp"

android:dashGap="3dp"

其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离

1.4 corners:圆角

android:radius为角的弧度,值越大角越圆。

我们还可以把四个角设定成不同的角度,

同时设置五个属性,则Radius属性无效

android:Radius="20dp" 设置四个角的半径

android:topLeftRadius="20dp" 设置左上角的半径

android:topRightRadius="20dp" 设置右上角的半径

android:bottomLeftRadius="20dp" 设置右下角的半径

android:bottomRightRadius="20dp" 设置左下角的半径

padding:间隔

可以设置上下左右四个方向的间隔

ps:为了方便交流请关注一下我的微博哦哦。

‘玖’ 怎么在android上面做出根据形状来渐变的效果

<shape> Android:shape=["rectangle" "oval" "line" "ring"] 其中rectagle矩形,oval椭圆,line水平直线,ring环形 <shape>中子节点的常用属性: <gradient> 渐变 Android:startColor 起始颜色 Android:endColor 结束颜色 Android:angle 渐变角度,0从左到右,90表示从下到上,数值为45的整数倍,默认为0; Android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep <solid > 填充 Android:color 填充的颜色 <stroke >描边 Android:width 描边的宽度 Android:color 描边的颜色 Android:dashWidth 表示'-'横线的宽度 Android:dashGap 表示'-'横线之间的距离 <corners >圆角 Android:radius 圆角的半径 值越大角越圆 Android:topRightRadius 右上圆角半径 Android:bottomLeftRadius 右下圆角角半径 Android:topLeftRadius 左上圆角半径 Android:bottomRightRadius 左下圆角半径 <padding >填充 android:bottom="1/apk/res/android"> <!-- 默认时的背景图片--> <item Android:drawable="@drawable/pic1" /> <!-- 没有焦点时的背景图片 --> <item Android:state_window_focused="false" android:drawable="@drawable/pic_blue" /> <!-- 非触摸模式下获得焦点并单击时的背景图片 --> <item Android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic_red" /> <!-- 触摸模式下单击时的背景图片--> <item Android:state_focused="false" Android:state_pressed="true" Android:drawable="@drawable/pic_pink" /> <!--选中时的图片背景--> <item Android:state_selected="true" android:drawable="@drawable/pic_orange" /> <!--获得焦点时的图片背景--> <item Android:state_focused="true" Android:drawable="@drawable/pic_green" /> </selector> layer-list(多个shape) 将多个图片或上面两种效果按照顺序层叠起来 示例: <?xml version="1/apk/res/android"> <item> <bitmap android:src="@drawable/android_red" android:gravity="center" /> </item> <item android:top="10dp" android:left="10dp"> <bitmap android:src="@drawable/android_green" android:gravity="center" /> </item> <item android:top="20dp" android:left="20dp"> <bitmap android:src="@drawable/android_blue" android:gravity="center" /> </item> </layer-list> 感觉很像多个drawable 三者可以结合使用 <selector xmlns:android="schemas/apk/res/android"> <item android:state_pressed="true"> <layer-list> <item android:bottom="8.0dip"> <shape> <solid android:color="#ffaaaaaa" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="#ffaaaaaa" /> <padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="@color/setting_item_bgcolor_press" /> </shape> </item> </layer-list> </item> <item> <layer-list> <item android:bottom="8.0dip"> <shape> <solid android:color="#ffaaaaaa" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="#ffaaaaaa" /> <padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="@color/setting_item_bgcolor" /> </shape> </item> </layer-list> </item> </selector>

阅读全文

与androidgradient渐变相关的资料

热点内容
程序员不时尚 浏览:745
不付费看网站 浏览:230
《代人受过》训诫文 浏览:258
1n.d5h49m6. 浏览:687
linuxweb目录权限 浏览:945
WWW 5a5e 浏览:544
id3v2java 浏览:222
怎么打开mysql命令行 浏览:522
linux文件星号 浏览:632
小城与小妈去北京旅游叫什么小说 浏览:230
pdf阅读器推荐 浏览:81
能免费看英语电影的软件 浏览:126
有部电影女主半夜出去卖淫 浏览:628
西门子编程试题 浏览:82
android转动360 浏览:334
服务器为什么要重装系统 浏览:438
华为尝鲜怎么还是原来的安卓系统 浏览:595
女主是警察的小说 浏览:792
魔兽宏命令是什么 浏览:250
《法国空姐》啄木鸟种子 浏览:95