/**
* Call this to create a new shader that will draw with a bitmap.
*
* @param bitmap The bitmap to use inside the shader
* @param tileX The tiling mode for x to draw the bitmap in.
* @param tileY The tiling mode for y to draw the bitmap in.
*/
public BitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY) {
......
}
package com.example.test;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
public class RounderCornerImageView extends View {
private Bitmap mImage;// source bitmap
private Paint mBitmapPaint;//paint
private RectF mBrounds;//rect
private float mRadius=20.0f;//round
public RounderCornerImageView(Context context) {
this(context, null);
}
public RounderCornerImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RounderCornerImageView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mBitmapPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
mBrounds=new RectF();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int height,width;
height=width=0;
//obtain bitmap size
int imageHeight,imageWidth;
if (null!=mImage) {
imageHeight=imageWidth=0;
}else
{
imageHeight=mImage.getHeight();
imageWidth=mImage.getWidth();
}
//obtain best measure data and set on View
width=getMeasurement(widthMeasureSpec,imageWidth);
height=getMeasurement(heightMeasureSpec, imageHeight);
//set View last size
setMeasuredDimension(width, height);
}
/**
* measure width and height by specMode
**/
private int getMeasurement(int measureSpec, int contentSize) {
int specSize=MeasureSpec.getSize(measureSpec);
switch (MeasureSpec.getMode(measureSpec)) {
case MeasureSpec.AT_MOST:
return Math.min(specSize, contentSize);
case MeasureSpec.UNSPECIFIED:
return contentSize;
case MeasureSpec.EXACTLY:
return specSize;
default:
return 0;
}//switch
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w!=oldw || h!=oldh) {
int imageWidth,imageHeight;
if (null==mImage) {
imageWidth=imageHeight=0;
}else
{
imageWidth=mImage.getWidth();
imageHeight=mImage.getHeight();
}
//center point
int left=(w-imageWidth)/2;
int top=(h-imageHeight)/2;
mBrounds.set(left, top, left+imageWidth, top+imageHeight);
if (null!=mBitmapPaint.getShader()) {
Matrix m=new Matrix();
m.setTranslate(left, top);
mBitmapPaint.getShader().setLocalMatrix(m);
}
}
}
public void setImage(Bitmap bitmap) {
if (mImage!=bitmap) {
mImage=bitmap;
if (null!=mImage) {
BitmapShader shader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mBitmapPaint.setShader(shader);
}else {
mBitmapPaint.setShader(null);
}
requestLayout();//invalidated the layout of this view by onDraw()
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (null!=mBitmapPaint) {
//draw Round Rect
canvas.drawRoundRect(mBrounds, mRadius, mRadius, mBitmapPaint);
}
}
}
package com.example.test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
public class RoundActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RounderCornerImageView view=new RounderCornerImageView(this);
Bitmap souBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.sun);
view.setImage(souBitmap);
setContentView(view);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有