Android 代码中设置Color参数


今天想绘制一条根据参数颜色动态变化的曲线,参数不固定,因此需要根据参数来获取颜色值。研究了很久,搞了一个通过Paint实现的方案。

Paint 中包含了很多方法对其属性进行设置,主要方法如下:

setAntiAlias: 设置画笔的锯齿效果。

setColor: 设置画笔颜色

setARGB:  设置画笔的a,r,p,g值。

setAlpha:  设置Alpha值

setTextSize: 设置字体尺寸。

setStyle:  设置画笔风格,空心或者实心。

setStrokeWidth: 设置空心的边框宽度。

getColor:  得到画笔的颜色

getAlpha:  得到画笔的Alpha值。

我们需要的就是setARGB和getColor。

private int getColor(float yourParam) {
		int alfa = 120;
		int rColor = 255;
		int gColor = 255;
		int bColor = 255;
		/*********
		 * Your code, change alfa/r/g/b by yourParam
		 *********/
		Paint paint = new Paint();
		paint.setARGB(alfa, rColor, gColor, bColor);
		return paint.getColor();
	}
时间: 2024-07-30 06:41:40

Android 代码中设置Color参数的相关文章

Android 代码中使用Color工具类 parseColor

方式一: arg1.setBackgroundColor(Color.parseColor("#87CEFA")); 方式二: arg1.setBackgroundColor(getResources().getColor(R.color.crowd));

Android代码中设置背景图片

//设置背景图片        String picfile= Environment.getExternalStorageDirectory() + "/pdp/pdp.png";        try {            Drawable d = Drawable.createFromPath(picfile);            linearLayout.setBackground(d);            //Toast.makeText(getApplicati

Android如何在java代码中设置margin

习惯了直接在xml里设置margin(距离上下左右都是10dip),如: <ImageView android:layout_margin="10dip" android:src="@drawable/image" /> 只是有些情况下,需要在java代码里来写. API中,android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom).可是View本身没

android 在代码中设置布局居中layout_gravity,layout_margin的方法

在代码中设置布局居中,翻看api可以知道view中有setGravity,setPadding,但是没有直接的setLayoutGravity,setMargin等方法.下面将在代码中实现类似布局中layout_gravity,layout_margin的方法. 可以通过设置view里面的 LayoutParams 设置,而这个LayoutParams是根据该view在不同的GroupView而不同的. 1.代码中设置layout_gravity LinearLayout layoutTop=(

获取屏幕高宽,在代码中设置控件大小的方法

获取屏幕高宽的方法: 1 import android.view.Display; 2 import android.view.WindowManager; 3 WindowManager windowManager = getWindowManager(); 4 Display display = windowManager.getDefaultDisplay(); 5 if(display.getWidth()==480 && display.getHeight()== 272 ||

从Android代码中来记忆23种设计模式

我的简书同步发布:从Android代码中来记忆23种设计模式 相信大家都曾经下定决心把23种设计模式牢记于心,每次看完之后过一段时间又忘记了~,又得回去看,脑子里唯一依稀记得的是少数设计模式的大致的定义.其实,网上很多文章讲得都非常好,我也曾经去看过各种文章.也曾一直苦恼这些难以永久记下的设计模式,直到我接触到了<Android源码设计模式解析与实战>--何红辉与关爱明著,发现原来其实我们在Android中都接触过这些设计模式,只是我们不知道而已.既然我们都接触过,我们只需一一对号入座,对设计

html代码中的form参数是基本一致的

由于pear的大多数模块仍处于开发当中,因此,这里列举的是随着php4.05一起发布的pear中的模块,需要注意的是,一些抽象类或者是基类(如mail.php,log.php,cache.php)没有列举出来,我们只是关注具有具体功能的模块.下面是这些模块的一个列表: benchmark/timer 测试你的一段php代码的运行效率 benchmark/benchmark_iterate 测试你某个函数循环执行时的性能 cache/output 可以将你的php脚本的输出进行缓存,可以使用多种方

Android代码中更改TextView颜色

项目中,需要在代码中动态更改TextView的颜色,原先使用如下: text.setTextColor(R.color.black); 为生效,查阅资料后,正确写法如下: text.setTextColor(context.getResources().getColor(R.color.black)); 或: text.setTextColor(getResources().getColorStateList(R.color.black)); Android代码中更改TextView颜色

【安卓】在java代码中设置drawableLeft时如何给定合适尺寸?

textView.setCompoundDrawables(drawable, null, null, null);时看不到图片,是因为需要手动给定drawable对应的尺寸,即用drawable.setBounds. 如果该drawable为图片,可直接drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());. 即直接给定图片自身尺寸,此时效果和在xml中给定一样. [安卓]在java