日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 仿微信聊天气泡显示图片,实现仿照微信聊天气泡里显示图片效果的自定义View...

發(fā)布時(shí)間:2024/10/8 编程问答 67 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 仿微信聊天气泡显示图片,实现仿照微信聊天气泡里显示图片效果的自定义View... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一部分是自定義的氣泡效果的View,代碼如下

public class BitmapShapeView extends View {

private Paint mPaint;

private Path mPath;//包裹圖片的那個(gè)不規(guī)則氣泡給需要自己用Path實(shí)現(xiàn)

private BitmapShader mBitmapShader;//填充起泡的渲染器

private Bitmap mBitmap;

private int mDefaultMinWidth = Constant.CHAT_MSG_IMG_DEFAULT_MIN_WIDTH;

private int mDefaultMinHeight = Constant.CHAT_MSG_IMG_DEFAULT_MIN_HEIGHT;

private int mBubbleRadius = 16;

private int mTriangleSize = 16;

private int mDirection = 2;

public BitmapShapeView(Context context, AttributeSet attrs) {

super(context, attrs);

mBitmap = BitmapUtils.resetBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.logo_gray), mDefaultMinWidth, mDefaultMinHeight);

mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

mPaint = new Paint();

mPath = new Path();

TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BubbleImageView);

mBubbleRadius = array.getDimensionPixelSize(R.styleable.BubbleImageView_bubbleRadius, 16);

mTriangleSize = array.getDimensionPixelSize(R.styleable.BubbleImageView_bubbleTriangleSize, 16);

mDirection = array.getInt(R.styleable.BubbleImageView_bubbleDirection, 2);

mDefaultMinWidth = array.getDimensionPixelSize(R.styleable.BubbleImageView_bubbleDefaultMinWidth, Constant.CHAT_MSG_IMG_DEFAULT_MIN_WIDTH);

mDefaultMinHeight = array.getDimensionPixelSize(R.styleable.BubbleImageView_bubbleDefaultMinHeight, Constant.CHAT_MSG_IMG_DEFAULT_MIN_HEIGHT);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.save();

mPaint.setStyle(Paint.Style.FILL);

mPaint.setShader(mBitmapShader);

canvas.drawPath(mPath, mPaint);

mPaint.reset();

canvas.restore();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (mBitmap != null) {

setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());

mPath.reset();

int width = getMeasuredWidth();

int height = getMeasuredHeight();

//畫三角

if (mDirection == 1) {//左三角

mPath.moveTo(mTriangleSize, height / 3 + mTriangleSize / 2);

mPath.lineTo(0, height / 3);

mPath.lineTo(mTriangleSize, height / 3 - mTriangleSize / 2);

mPath.arcTo(new RectF(mTriangleSize, 0, mTriangleSize + mBubbleRadius * 2, mBubbleRadius * 2), 180, 90);//起始位置的角度值,旋轉(zhuǎn)的角度值

mPath.lineTo(width - mBubbleRadius, 0);

mPath.arcTo(new RectF(width - mBubbleRadius * 2, 0, width, mBubbleRadius * 2), -90, 90);

mPath.lineTo(width, height - mBubbleRadius);

mPath.arcTo(new RectF(width - 2 * mBubbleRadius, height - mBubbleRadius * 2, width, height), 0, 90);

mPath.lineTo(mTriangleSize + mBubbleRadius, height);

mPath.arcTo(new RectF(mTriangleSize, height - mBubbleRadius * 2, mTriangleSize + mBubbleRadius * 2, height), 90, 90);

mPath.close();

} else if (mDirection == 2) {//右三角

mPath.moveTo(0, mBubbleRadius);

mPath.arcTo(new RectF(0, 0, mBubbleRadius * 2, mBubbleRadius * 2), 180, 90);//起始位置的角度值,旋轉(zhuǎn)的角度值

mPath.lineTo(width - mBubbleRadius + mTriangleSize, 0);

mPath.arcTo(new RectF(width - mBubbleRadius * 2 - mTriangleSize, 0, width - mTriangleSize, mBubbleRadius * 2), -90, 90);

mPath.lineTo(width - mTriangleSize, height / 3 - mTriangleSize / 2);

mPath.lineTo(width, height / 3);

//??????????????? mPath.lineTo(width-1,height/3-1);

//??????????????? mPath.arcTo(new RectF(width-4,height/3-4, width, height/3+4), -90, 180);

mPath.lineTo(width - mTriangleSize, height / 3 + mTriangleSize / 2);

mPath.lineTo(width - mTriangleSize, height - mBubbleRadius);

mPath.arcTo(new RectF(width - 2 * mBubbleRadius - mTriangleSize, height - mBubbleRadius * 2, width - mTriangleSize, height), 0, 90);

mPath.lineTo(mBubbleRadius, height);

mPath.arcTo(new RectF(0, height - mBubbleRadius * 2, mBubbleRadius * 2, height), 90, 90);

mPath.close();

}

}

}

public void setImageSrc(String path) {

mBitmap = BitmapUtils.resetBitmap(BitmapFactory.decodeFile(path), mDefaultMinWidth, mDefaultMinHeight);

mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

requestLayout();

invalidate();

}

public void setImageSrc(int resId) {

mBitmap = BitmapUtils.resetBitmap(BitmapFactory.decodeResource(getResources(), resId), mDefaultMinWidth, mDefaultMinHeight);

mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

requestLayout();

invalidate();

}

public void setImageBitmap(Bitmap bitmap) {

mBitmap = BitmapUtils.resetBitmap(bitmap, mDefaultMinWidth, mDefaultMinHeight);

mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

requestLayout();//一定得加上這方法,否則當(dāng)加載不同的View時(shí),View方法的onMeasure不會(huì)重新調(diào)用

invalidate();

}

}

第二部分是屬性值部分,設(shè)置氣泡的圓角,氣泡三角的大小,圖片顯示最小的寬高值,代碼如下:

最后只要在你的layout文件里使用這自定義的View就能顯示氣泡圖片了。初次寫,代碼不完善的地方希望同學(xué)們能不吝賜教啊,O(∩_∩)O

總結(jié)

以上是生活随笔為你收集整理的android 仿微信聊天气泡显示图片,实现仿照微信聊天气泡里显示图片效果的自定义View...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。