时间:2021-05-20
现在我们就来看看怎么样把图片的四角都变成圆形的,为什么要这样做那,如果要是这样界面就会非常的美观,下面我们就来看看代码吧。
java代码:
复制代码 代码如下:
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;
}
当我们需要圆角的时候,调用这个方法,第一个参数是传入需要转化成圆角的图片,第二个参数是圆角的度数,数值越大,圆角越大
下面兴趣个例子,我有一个LinearLayout,我想把它的背景图片设置成圆角
布局文件main.xml
java代码:
复制代码 代码如下:
<?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>
java代码:
复制代码 代码如下:
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;
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
今天处理了一个页面刷新随机显示图片的功能,发现直角太丑,想实现图片圆角,本来想用php实现的,但是有点麻烦,发现了一个很好的css解决方案,立马用上:css实现
Android使用帧动画内存溢出解决方案最近在项目遇到的动画效果不好实现,就让UI切成图,采用帧动画实现效果,但是在使用animation-list时,图片也就
Android使用xml自定义图片实例详解实现效果图:白色圆角图片bg_round_rectangle_white.xml白色圆角虚线框bg_round_rec
利用PIL将图片转换为黑色与白色反转的图片,下面笔者小白介绍如何实现。解决方案一:fromPILimportImageimportPIL.ImageOps#读入
AndroidListView异步加载图片错位、重复、闪烁分析以及解决方案,具体问题分析以及解决方案请看下文。我们在使用ListView异步加载图片的时候,在快