本帖最后由 liu 于 2018-12-23 18:49 编辑
使用到圆形的图片,刚开始的时候不知道怎么做,都是使用一些第三方框架,其实Android提供的有类:supportV4下的RoundedBitmapDrawable,使用它可以很方便地实现圆角或者圆角半径
示例代码:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageView
android:id="@+id/iv2"
android:layout_marginTop="20dp"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
MainActivity:
[Java] 纯文本查看 复制代码 package com.example.asus.myapplication;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.iv);
ImageView imageView2 = (ImageView) findViewById(R.id.iv2);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.a));
//将图片设置成圆角
roundedBitmapDrawable.setCircular(true);
imageView.setImageDrawable(roundedBitmapDrawable);
RoundedBitmapDrawable roundedBitmapDrawable2 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.a));
//设置图片圆角半径
roundedBitmapDrawable2.setCornerRadius(50);
imageView2.setImageDrawable(roundedBitmapDrawable2);
}
}
看下效果图:
|