如何创建具有形状圆形的ImageView的,有一个边界? [英] How to create an imageView with shape circle and have a border?

查看:204
本文介绍了如何创建具有形状圆形的ImageView的,有一个边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要与具有的ImageView 需要为一个圆(由croping squre图片)创建一个Android应用程序。我用计算器,从一些编码,但我不知道添加边框给它。

编码如下:

 包com.fidenz.fexceller.fexceller;/ **
 *创建者Chathu Hettiarachchi于2015年5月18日。
 * /
进口android.graphics.Bitmap;
进口android.graphics.BitmapShader;
进口android.graphics.Canvas;
进口android.graphics.ColorFilter;
进口android.graphics.Paint;
进口android.graphics.PixelFormat;
进口android.graphics.Rect;
进口android.graphics.RectF;
进口android.graphics.Shader;
进口android.graphics.drawable.Drawable;公共类RoundedImg扩展可绘制{
    私人最终位图mBitmap;
    私人最终涂料mPaint;
    私人最终RectF mRectF;
    私人最终诠释mBitmapWidth;
    私人最终诠释mBitmapHeight;    公共RoundedImg(位图位图){
        mBitmap =位图;
        mRectF =新RectF();
        mPaint =新的油漆();
        mPaint.setAntiAlias​​(真);
        mPaint.setDither(真);
        最后BitmapShader着色器=新BitmapShader(位图,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        mPaint.setShader(着色器);        mBitmapWidth = mBitmap.getWidth();
        mBitmapHeight = mBitmap.getHeight();
    }    @覆盖
    公共无效画(油画画布){
        canvas.drawOval(mRectF,mPaint);
    }    @覆盖
    保护无效onBoundsChange(矩形边界){
        super.onBoundsChange(边界);
        mRectF.set(边界);
    }    @覆盖
    公共无效setAlpha(INT阿尔法){
        如果(mPaint.getAlpha()!=阿尔法){
            mPaint.setAlpha(阿尔法);
            invalidateSelf();
        }
    }    @覆盖
    公共无效setColorFilter(ColorFilter CF){
        mPaint.setColorFilter(CF);
    }    @覆盖
    公众诠释getOpacity(){
        返回PixelFormat.TRANSLUCENT;
    }    @覆盖
    公众诠释getIntrinsicWidth(){
        返回mBitmapWidth;
    }    @覆盖
    公众诠释getIntrinsicHeight(){
        返回mBitmapHeight;
    }    公共无效setAntiAlias​​(布尔AA){
        mPaint.setAntiAlias​​(AA);
        invalidateSelf();
    }    @覆盖
    公共无效setFilterBitmap(布尔过滤器){
        mPaint.setFilterBitmap(过滤器);
        invalidateSelf();
    }    @覆盖
    公共无效setDither(布尔抖动){
        mPaint.setDither(抖动);
        invalidateSelf();
    }    公共位图getBitmap(){
        返回mBitmap;
    }}

我呼吁使用这个图片:

  ImageView的profilePic;
RoundedImg roundedImage,环;profilePic =(ImageView的)findViewById(R.id.img_home_profile_pic);        位图BM = BitmapFactory.de codeResource(getResources(),R.drawable.no_image);
        roundedImage =新RoundedImg(BM);
        profilePic.setImageDrawable(roundedImage);


解决方案

试着用这样的:

  ImageView的profilePic;
RoundedImg roundedImage,环;位图BM = BitmapFactory.de codeResource(getResources(),R.drawable.no_image);
位图withBorder = addBlackBorder(宽多重峰,5);
roundedImage =新RoundedImg(withBorder);
profilePic.setImageDrawable(roundedImage); 私人位图addBlackBorder(BMP位图,诠释borderSize){
    位图bmpWithBorder = Bitmap.createBitmap(bmp.getWidth()+ borderSize * 2,bmp.getHeight()+ borderSize * 2,bmp.getConfig());
    帆布帆布=新的Canvas(bmpWithBorder);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(BMP,borderSize,borderSize,NULL);
    返回bmpWithBorder;
}

I need to create an android app with having an imageview that need to be a circle (by croping squre image). I have used some coding from StackOverFlow but I've no idea to add a border to it.

Coding below:

package com.fidenz.fexceller.fexceller;

/**
 * Created by Chathu Hettiarachchi on 5/18/2015.
 */
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.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

public class RoundedImg extends Drawable {
    private final Bitmap mBitmap;
    private final Paint mPaint;
    private final RectF mRectF;
    private final int mBitmapWidth;
    private final int mBitmapHeight;

    public RoundedImg(Bitmap bitmap) {
        mBitmap = bitmap;
        mRectF = new RectF();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);

        mBitmapWidth = mBitmap.getWidth();
        mBitmapHeight = mBitmap.getHeight();
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawOval(mRectF, mPaint);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mRectF.set(bounds);
    }

    @Override
    public void setAlpha(int alpha) {
        if (mPaint.getAlpha() != alpha) {
            mPaint.setAlpha(alpha);
            invalidateSelf();
        }
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmapWidth;
    }

    @Override
    public int getIntrinsicHeight() {
        return mBitmapHeight;
    }

    public void setAntiAlias(boolean aa) {
        mPaint.setAntiAlias(aa);
        invalidateSelf();
    }

    @Override
    public void setFilterBitmap(boolean filter) {
        mPaint.setFilterBitmap(filter);
        invalidateSelf();
    }

    @Override
    public void setDither(boolean dither) {
        mPaint.setDither(dither);
        invalidateSelf();
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

}

And I call image using this:

ImageView profilePic;
RoundedImg roundedImage,ring;

profilePic = (ImageView)findViewById(R.id.img_home_profile_pic);

        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.no_image);
        roundedImage = new RoundedImg(bm);
        profilePic.setImageDrawable(roundedImage);

解决方案

Try with something like this:

ImageView profilePic;
RoundedImg roundedImage,ring;

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.no_image);
Bitmap withBorder = addBlackBorder(bm, 5);
roundedImage = new RoundedImg(withBorder);
profilePic.setImageDrawable(roundedImage);

 private Bitmap addBlackBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}

这篇关于如何创建具有形状圆形的ImageView的,有一个边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆