android 设置圆角图片实现代码

时间:2023-11-29 15:00:29 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
package com.test.demo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Drawable drawable = getResources().getDrawable(R.drawable.bg);
// BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
// Bitmap bitmap = bitmapDrawable.getBitmap();

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
Drawable drawable = getResources().getDrawable(R.drawable.bg);
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();

BitmapDrawable bbb = new BitmapDrawable(toRoundCorner(bitmap, 30));
layout.setBackgroundDrawable(bbb);
//ImageView imageView = (ImageView) findViewById(R.id.imgShow);
//imageView.setImageBitmap(MyActivity.getRoundedCornerBitmap(bitmap));
//imageView.setImageBitmap(MyActivity.toRoundCorner(bitmap, 20));
}
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}
}
标签:android,设置,圆角图片
0
投稿

猜你喜欢

  • java+SpringBoot设计实现评教系统

    2023-02-05 13:12:18
  • C# PropertyGrid使用案例详解

    2022-03-18 20:51:51
  • 教你怎么用java一键自动生成数据库文档

    2021-08-01 02:34:36
  • C#实现Winform版计算器

    2023-01-25 06:59:37
  • EventBus与Spring Event区别详解(EventBus 事件机制,Spring Event事件机制)

    2023-12-06 00:23:05
  • C#编程获取客户端计算机硬件及系统信息功能示例

    2023-08-09 01:57:15
  • Java实现常见排序算法的优化

    2022-05-20 13:08:37
  • Java 集合概览(小结)

    2023-11-08 02:50:37
  • MyBatis中不建议使用where 1=1原因详解

    2021-08-09 23:25:55
  • C#中使用UDP通信的示例

    2022-11-19 21:09:07
  • MyBatis注解实现动态SQL问题

    2023-08-19 02:34:09
  • SpringMVC中@ModelAttribute与@RequestBody的区别及说明

    2023-11-24 12:09:51
  • 详解Mybatis Generator的具体使用教程

    2022-01-16 22:39:13
  • sublime text3搭建配置c语言编译环境的详细图解教程(小白级)

    2021-12-16 09:18:27
  • 教你使用springSecurity+jwt实现互踢功能

    2023-05-24 19:45:10
  • Spring Cloud Alibaba Nacos Config加载配置详解流程

    2022-03-24 23:06:01
  • Spring-Data-JPA整合MySQL和配置的方法

    2023-10-29 10:19:41
  • Java常问面试内容--数组、声明、初始化、冒泡、多维数组、稀疏数组

    2021-08-20 02:05:52
  • C#判断一个字符串是否是数字或者含有某个数字的方法

    2022-05-07 14:38:17
  • Java中ShardingSphere分库分表实战

    2023-11-24 09:20:37
  • asp之家 软件编程 m.aspxhome.com