自定义view与自定义drawable在设置图像中的使用

自定义View

先使用BitmapFactory.decodeResource()来获取一个位图Bitmap,其中第一个参数为资源,第二个参数为要修改的图片

然后使用Shader对其进行操作

对于BitmapShader有几种模式{CLAMP拉伸,REPEAT重复,MIRROR镜像},这里选CLAMP

然后将获得的bitmap绘制成圆形

这样便可以在布局中使用这个view了

代码如下


import android.view.View;

import com.example.administrator.copyqq01.R;

/**
 * Created by Administrator on 2016/6/7.
 * 实现image的圆形显示
 */
public class ImageViewRound extends View {

    private Bitmap bitmap;
    private Paint paint;
    private BitmapShader bitmapShader;

    public ImageViewRound(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


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


    public ImageViewRound(Context context) {
        super(context);
    }

    //如何动态的加载R.drawable.get2资源?
    @Override
    protected void onDraw(Canvas canvas) {
        bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.get2);
        bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        paint=new Paint();
        paint.setShader(bitmapShader);
        canvas.drawCircle(50,50,50,paint);
    }
}

自定义Drawable

继承一个Drawable,在其中的构造函数中对传入的Bitmap进行设置,在布局中加入一个ImageView(或者其他什么的),然后在代码中对imageView进行设置

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

/**
 * Created by Administrator on 2016/7/3.
 */
public class RoundImageDrawable extends Drawable {

    private Paint mPaint;
    private Bitmap mBitmap;
    private RectF rectF;

    public RoundImageDrawable(Bitmap bitmap){
        mBitmap=bitmap;
        BitmapShader bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint=new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setShader(bitmapShader);
    }

    @Override
    public void setBounds(int left,int top,int right,int bottom) {
        super.setBounds(left,top,right,bottom);
        rectF=new RectF(left,top,right,bottom);
    }

    @Override
    public void draw(Canvas canvas) {

        canvas.drawRoundRect(rectF,30,30,mPaint);
    }

    @Override
    public int getIntrinsicHeight() {
        return mBitmap.getHeight();
    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmap.getWidth();
    }

    @Override
    public void setAlpha(int alpha) {

        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {

        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;//设置透明
    }
}

Mainactivity中设置

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;


/**
 * Created by Administrator on 2016/3/7.
 */
public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.mv);
        ImageView imageView=(ImageView)findViewById(R.id.id_one);
        imageView.setImageDrawable(new RoundImageDrawable(bitmap));
    }
}

参考

http://www.cnblogs.com/tianzhijiexian/p/4298660.html

http://blog.csdn.net/lmj623565791/article/details/43752383




发布了177 篇原创文章 · 获赞 69 · 访问量 36万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览