//1.为文字添加阴影 2.为drawable 添加滤镜
public class ColorFilters extends GraphicsActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new SampleView(this));}private static class SampleView extends View {private Activity mActivity;private Drawable mDrawable;private Drawable[] mDrawables;private Paint mPaint;private Paint mPaint2;private float mPaintTextOffset;private int[] mColors;private PorterDuff.Mode[] mModes;private int mModeIndex;private static void addToTheRight(Drawable curr, Drawable prev) {Rect r = prev.getBounds();int x = r.right + 12;int center = (r.top + r.bottom) >> 1;int h = curr.getIntrinsicHeight();int y = center - (h >> 1);curr.setBounds(x, y, x + curr.getIntrinsicWidth(), y + h);}public SampleView(Activity activity) {super(activity);mActivity = activity;Context context = activity;setFocusable(true);mDrawable = context.getResources().getDrawable(R.drawable.btn_default_normal);mDrawable.setBounds(0, 0, 150, 48);
// mDrawable.setDither(true);int[] resIDs = new int[] {R.drawable.btn_circle_normal,R.drawable.btn_check_off,R.drawable.btn_check_on};mDrawables = new Drawable[resIDs.length];Drawable prev = mDrawable;for (int i = 0; i < resIDs.length; i++) {mDrawables[i] = context.getResources().getDrawable(resIDs[i]);mDrawables[i].setDither(true);addToTheRight(mDrawables[i], prev);prev = mDrawables[i];}mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setTextSize(16);mPaint.setTextAlign(Paint.Align.CENTER);mPaint2 = new Paint(mPaint);mPaint2.setAlpha(64);Paint.FontMetrics fm = mPaint.getFontMetrics();mPaintTextOffset = (fm.descent + fm.ascent) * 0.5f; //计算字母的上下 超出部分居中mColors = new int[] {0,0xCC0000FF,0x880000FF,0x440000FF,0xFFCCCCFF,0xFF8888FF,0xFF4444FF,};mModes = new PorterDuff.Mode[] { //具体模式 参考http://blog.csdn.net/starfeng11/article/details/7000284PorterDuff.Mode.SRC_ATOP,PorterDuff.Mode.MULTIPLY,};mModeIndex = 0;updateTitle();}private void swapPaintColors() { //切换 画笔的颜色if (mPaint.getColor() == 0xFF000000) {mPaint.setColor(0xFFFFFFFF);mPaint2.setColor(0xFF000000);} else {mPaint.setColor(0xFF000000);mPaint2.setColor(0xFFFFFFFF);}mPaint2.setAlpha(64);}private void updateTitle() {mActivity.setTitle(mModes[mModeIndex].toString());}private void drawSample(Canvas canvas, ColorFilter filter) {Rect r = mDrawable.getBounds();float x = (r.left + r.right) * 0.5f;float y = (r.top + r.bottom) * 0.5f - mPaintTextOffset;mDrawable.setColorFilter(filter);mDrawable.draw(canvas);canvas.drawText("Label", x+1, y+1, mPaint2);//这是用来给字体设置阴影的canvas.drawText("Label", x, y, mPaint);for (Drawable dr : mDrawables) {dr.setColorFilter(filter);dr.draw(canvas);}}@Override protected void onDraw(Canvas canvas) {canvas.drawColor(0xFFCCCCCC);canvas.translate(8, 12);//在 8,12 位置drawfor (int color : mColors) {ColorFilter filter;if (color == 0) {filter = null;} else {filter = new PorterDuffColorFilter(color,mModes[mModeIndex]);//添加不同颜色的滤镜}drawSample(canvas, filter);canvas.translate(0, 55);//绘制完了一个 下移55px}}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_MOVE:break;case MotionEvent.ACTION_UP:// update mode every other time we change paint colorsif (mPaint.getColor() == 0xFFFFFFFF) {mModeIndex = (mModeIndex + 1) % mModes.length;updateTitle();}swapPaintColors();invalidate();break;}return true;}}
}
ColorMatrix不贴代码 看这
对图像进行颜色方面的处理,通过使用颜色矩阵(ColorMatrix)来实现。从而可以达到很多特效如黑白老照片、泛黄旧照片等等。
1.颜色矩阵(ColorMatrix)
这里有详细的介绍:http://developer.android.com/reference/android/graphics/ColorMatrix.html
不过是英文的,在这里我就先导读一下。
一张位图可以转换为一个5*4的矩阵,涉及到颜色和透明度。如图1所示。在Android中,颜色矩阵M是以一维数组m=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]的方式进行存储的。
图1
在一张图片中,图像的RGBA(红色、绿色、蓝色、透明度)值决定了该图片所呈现出来的颜色效果。
而图像的RGBA值则存储在一个5*1的颜色分量矩阵C中,由颜色分量矩阵C可以控制图像的颜色效果。颜色分量矩阵C如图2所示。
图2
要想改变一张图片的颜色效果,只需要改变图像的颜色分量矩阵即可。通过颜色矩阵可以很方便的修改图像的颜色分量矩阵。假设修改后的图像颜色分量矩阵为C1,则有如图3所示的颜色分量矩阵计算公式。
图3
由此可见,通过颜色矩阵修改了原图像的RGBA值,从而达到了改变图片颜色效果的目的。并且,通过如图3所示的运算可知,颜色矩阵M的第一行参数abcde决定了图像的红色成分,第二行参数fghij决定了图像的绿色成分,第三行参数klmno决定了图像的蓝色成分,第四行参数pqrst决定了图像的透明度,第五列参数ejot是颜色的偏移量。
通常,改变颜色分量时可以通过修改第5列的颜色偏移量来实现,如图4所示的颜色矩阵M1,通过计算后可以得知该颜色矩阵的作用是使图像的红色分量和绿色分量均增加100,这样的效果就是图片泛黄(因为红色与绿色混合后得到黄色)。
图4
除此之外,也可以通过直接对颜色值乘以某一系数而达到改变颜色分量的目的。如图5所示的颜色矩阵M2,将绿色分量放大了2倍,这样的效果就是图片泛绿色。
图5
实例:
步骤一:我们首先自定义一个view,用来显示我们处理的图片。
ColorView.java
- package com.mycolor;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.ColorMatrix;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.ImageView;
- public class ColorView extends ImageView {
- private Paint myPaint = null;
- private Bitmap bitmap = null;
- private ColorMatrix myColorMatrix = null;
- private float[] colorArray = { 1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0};
- public ColorView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.a2);
- invalidate();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- //新建画笔对象
- myPaint = new Paint();
- //描画(原始图片)
- canvas.drawBitmap(bitmap,0, 0, myPaint);
- //新建颜色矩阵对象
- myColorMatrix = new ColorMatrix();
- //设置颜色矩阵的值
- myColorMatrix.set(colorArray);
- //设置画笔颜色过滤器
- myPaint.setColorFilter(new ColorMatrixColorFilter(myColorMatrix));
- //描画(处理后的图片)
- canvas.drawBitmap(bitmap,0,0,myPaint);
- invalidate();
- }
- //设置颜色数值
- public void setColorArray(float[] colorArray){
- this.colorArray = colorArray;
- }
- //设置图片
- public void setBitmap(Bitmap bitmap){
- this.bitmap = bitmap;
- }
- }
步骤二:自定义我们的布局
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/colorView_layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <com.mycolor.ColorView
- android:id="@+id/myColorView"
- android:layout_width="480dp"
- android:layout_height="180dp"/>
- <LinearLayout
- android:id="@+id/colorlayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit1"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit2"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0"
- />
- <EditText
- android:id="@+id/Edit3"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit4"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit5"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit6"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit7"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit8"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit9"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit10"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit11"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit12"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit13"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit14"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit15"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit16"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit17"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit18"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit19"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit20"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <Button
- android:id="@+id/Button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="0dp"
- android:text="提交" />
- </LinearLayout>
步骤三:完成我们的Activity
- package com.mycolor;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class ColorActivity extends Activity implements OnClickListener{
- private Button button = null;
- private ColorView colorView = null;
- private EditText[] editTextArray = null;
- private float colorArray[] = null;
- private int[] EditTextID = {R.id.Edit1,R.id.Edit2,R.id.Edit3,R.id.Edit4,R.id.Edit5,
- R.id.Edit6,R.id.Edit7,R.id.Edit8,R.id.Edit9,R.id.Edit10,
- R.id.Edit11,R.id.Edit12,R.id.Edit13,R.id.Edit14,R.id.Edit15,
- R.id.Edit16,R.id.Edit17,R.id.Edit18,R.id.Edit19,R.id.Edit20};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.Button);
- button.setOnClickListener(this);
- editTextArray = new EditText[20];
- colorArray = new float[20];
- for(int i = 0;i < 20;i++){
- editTextArray[i] = (EditText)findViewById(EditTextID[i]);
- }
- colorView = (ColorView)findViewById(R.id.myColorView);
- }
- @Override
- public void onClick(View v) {
- for(int i = 0;i < 20;i++){
- colorArray[i] = Float.valueOf(editTextArray[i].getText().toString().trim());
- System.out.println("i = " + i + ":" + editTextArray[i].getText().toString().trim());
- }
- colorView.setColorArray(colorArray);
- }
- }
这样就可以了。
效果图:
改变值可以呈现不同的效果:
原文参考:点击打开链接
代码:点击下载
public class Compass extends GraphicsActivity {private static final String TAG = "Compass";private SensorManager mSensorManager;private Sensor mSensor;private SampleView mView;private float[] mValues;private final SensorEventListener mListener = new SensorEventListener() {public void onSensorChanged(SensorEvent event) {if (Config.DEBUG) Log.d(TAG,"sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");mValues = event.values; //旋转的值,if (mView != null) {mView.invalidate();}}public void onAccuracyChanged(Sensor sensor, int accuracy) {}};@Overrideprotected void onCreate(Bundle icicle) {super.onCreate(icicle);mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);mView = new SampleView(this);setContentView(mView);}@Overrideprotected void onResume(){if (Config.DEBUG) Log.d(TAG, "onResume");super.onResume();mSensorManager.registerListener(mListener, mSensor,SensorManager.SENSOR_DELAY_GAME);}@Overrideprotected void onStop(){if (Config.DEBUG) Log.d(TAG, "onStop");mSensorManager.unregisterListener(mListener);super.onStop();}private class SampleView extends View {private Paint mPaint = new Paint();private Path mPath = new Path();private boolean mAnimate;public SampleView(Context context) {super(context);// Construct a wedge-shaped path//绘制一个箭头mPath.moveTo(0, -50);mPath.lineTo(-20, 60);mPath.lineTo(0, 50);mPath.lineTo(20, 60);mPath.close();}@Override protected void onDraw(Canvas canvas) {Paint paint = mPaint;canvas.drawColor(Color.WHITE);paint.setAntiAlias(true);paint.setColor(Color.BLACK);paint.setStyle(Paint.Style.FILL);int w = canvas.getWidth();//居中int h = canvas.getHeight();int cx = w / 2;int cy = h / 2;canvas.translate(cx, cy);if (mValues != null) {canvas.rotate(-mValues[0]);}canvas.drawPath(mPath, mPaint);}@Overrideprotected void onAttachedToWindow() {mAnimate = true;if (Config.DEBUG) Log.d(TAG, "onAttachedToWindow. mAnimate=" + mAnimate);super.onAttachedToWindow();}@Overrideprotected void onDetachedFromWindow() {mAnimate = false;if (Config.DEBUG) Log.d(TAG, "onDetachedFromWindow. mAnimate=" + mAnimate);super.onDetachedFromWindow();}}
}