android圖片圓形實(shí)現(xiàn)方式

此篇是我圓形圖片學(xué)習(xí)總結(jié)的文章,最終實(shí)現(xiàn)的效果圖如下

文章參考

Android圓形圖片不求人,自定義View實(shí)現(xiàn)(BitmapShader使用)
Android - 實(shí)現(xiàn)圖片圓角顯示的幾種方式
Android 完美實(shí)現(xiàn)圖片圓角和圓形(對(duì)實(shí)現(xiàn)進(jìn)行分析)

result.png

z.jpg

核心代碼
class LoadTask1 extends AsyncTask<Void, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Void... params) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);
            //這里是獲取到原圖
            Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.drawable.z);
            //我通過getWidth,getHeight獲取到了寬高
            Bitmap result = Bitmap.createBitmap(bitmapSource.getWidth(), bitmapSource.getHeight(), Bitmap.Config.ARGB_8888);
            //創(chuàng)建一個(gè)畫布
            Canvas canvas = new Canvas(result);
            //通過寬高比,獲取到最小的那個(gè)值
            int min = 0;
            if (result.getWidth() > result.getHeight()){
                min = result.getHeight();
            }else if (result.getWidth() < result.getHeight()){
                min = result.getWidth();
            }else {
                min = result.getWidth();
            }
            canvas.drawCircle(min/2,min/2,min/2,paint);//先畫一個(gè)圓
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//表示我下一步要取交集的地方
            canvas.drawBitmap(bitmapSource, 0, 0, paint);//又畫一個(gè)圖,并且這個(gè)圖是在圓形的上面,此時(shí)就是獲取到交集的地方
            return result;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);
        }
    }
核心思想

首先是畫了一個(gè)圓形圖片通過canvas.drawCircle方法
PorterDuff.Mode.SRC_IN:指明了取交集的部分
canvas.drawBitmap:又創(chuàng)建一個(gè)圖片,這個(gè)圖片是在圓形圖之上的,此時(shí)就會(huì)有交集部分了

全部代碼
public class MainActivity extends AppCompatActivity {

    int imageSize, radius;

    ImageView imageView;
    ImageView imageView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image);
        imageView2 = (ImageView) findViewById(R.id.image2);

        imageSize = getResources().getDimensionPixelSize(R.dimen.image_size);
        radius = getResources().getDimensionPixelSize(R.dimen.radius);

        new LoadTask1().execute();
        new LoadTask2().execute();
    }

    class LoadTask1 extends AsyncTask<Void, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Void... params) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);
            //這里是獲取到原圖
            Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.drawable.z);
            //我通過getWidth,getHeight獲取到了寬高
            Bitmap result = Bitmap.createBitmap(bitmapSource.getWidth(), bitmapSource.getHeight(), Bitmap.Config.ARGB_8888);
            //創(chuàng)建一個(gè)畫布
            Canvas canvas = new Canvas(result);
            //通過寬高比,獲取到最小的那個(gè)值
            int min = 0;
            if (result.getWidth() > result.getHeight()){
                min = result.getHeight();
            }else if (result.getWidth() < result.getHeight()){
                min = result.getWidth();
            }else {
                min = result.getWidth();
            }
            canvas.drawCircle(min/2,min/2,min/2,paint);//先畫一個(gè)圓
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//表示我下一步要取交集的地方
            canvas.drawBitmap(bitmapSource, 0, 0, paint);//又畫一個(gè)圖,并且這個(gè)圖是在圓形的上面,此時(shí)就是獲取到交集的地方
            return result;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);
        }
    }

    class LoadTask2 extends AsyncTask<Void, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Void... params) {
            Bitmap result = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.an);

            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(0, 0, imageSize, imageSize);

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);


            Path path = new Path();
            path.addRoundRect(rectF, radius, radius, Path.Direction.CW);
            canvas.clipPath(path, Region.Op.INTERSECT);

            canvas.drawBitmap(bitmap, rect, rectF, paint);

            return result;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            imageView2.setImageBitmap(bitmap);
        }
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容