android 设置圆角图片实现代码
短信预约 -IT技能 免费直播动态提醒
代码如下:
<?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 {
@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.class="lazy" data-src_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
您可能感兴趣的文章:android 实现圆角图片解决方案Android图片特效:黑白特效、圆角效果、高斯模糊Android关于Glide的使用(高斯模糊、加载监听、圆角图片)Android中TextView显示圆圈背景或设置圆角的方法Android中实现EditText圆角的方法Android中Glide加载圆形图片和圆角图片实例代码Android开发使用自定义View将圆角矩形绘制在Canvas上的方法Android实现圆角矩形和圆形ImageView的方式Android中实现圆角图片的几种方法Android切圆角的常见方式总结
<?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 {
@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.class="lazy" data-src_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
您可能感兴趣的文章:android 实现圆角图片解决方案Android图片特效:黑白特效、圆角效果、高斯模糊Android关于Glide的使用(高斯模糊、加载监听、圆角图片)Android中TextView显示圆圈背景或设置圆角的方法Android中实现EditText圆角的方法Android中Glide加载圆形图片和圆角图片实例代码Android开发使用自定义View将圆角矩形绘制在Canvas上的方法Android实现圆角矩形和圆形ImageView的方式Android中实现圆角图片的几种方法Android切圆角的常见方式总结
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341