最近在学习对图片的操作~android里有个shader类可以对图片进行各种酷炫操作,今天就和大家分享一下吧.若有写错的地方还望大家指出来,一起学习,学习
下面就是我做的一个小demo
package com.example.shaderdemo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;
@SuppressLint({ "DrawAllocation", "ViewConstructor" })
public class MyView extends View {
private Bitmap bitmap;
private int bitHeight;
private int bitWidth;
//声明画笔
private Paint paint;
//bitmap渲染
private Shader bitmapShader;
//线性渐变渲染
private Shader linearGradient;
//混合渲染
private Shader composeShader;
//圆形渐变渲染
private Shader radialGradient;
//梯度渲染
private Shader sweepGradient;
private ShapeDrawable shapeDrawable;
public MyView(Context context,Bitmap bitmap) {
super(context);
this.bitmap = bitmap;
init();
}
/**
* 初始化数据
*/
private void init() {
//获取图片的高度和宽度
bitHeight = bitmap.getHeight();
bitWidth = bitmap.getWidth();
paint = new Paint();
int[] colors = {Color.RED,Color.WHITE,Color.GRAY,Color.BLUE};
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
linearGradient = new LinearGradient(0, 0, 100, 100, colors, null, Shader.TileMode.MIRROR);
radialGradient = new RadialGradient(50, 200, 50, colors, null, Shader.TileMode.REPEAT);
composeShader = new ComposeShader(bitmapShader, linearGradient, PorterDuff.Mode.DARKEN);
sweepGradient = new SweepGradient(30, 30, colors, null);
//进行刷新界面
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setShader(bitmapShader);
shapeDrawable.setBounds(0, 0, bitWidth/2, bitHeight/2);
shapeDrawable.draw(canvas);
paint.setShader(linearGradient);
canvas.drawRect(bitWidth,0,320,156, paint);
paint.setShader(composeShader);
canvas.drawRect(0, 500, bitWidth/2, 400+bitHeight/2, paint);
paint.setShader(radialGradient);
canvas.drawCircle(50, 200, 50, paint);
paint.setShader(sweepGradient);
canvas.drawRect(150, 160, 300, 300, paint);
}
}
package com.example.shaderdemo;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.lf)).getBitmap();
MyView myView = new MyView(this, bitmap);
rl.addView(myView);
}
}
//哈哈下面就xml布局了,什么都没有就一个relativeLayout
<RelativeLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
xmlns:tools="<a target=_blank href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl"
tools:context=".MainActivity" >
</RelativeLayout>