使用getDrawingCache发布android的圆角imageview

时间:2016-08-23 04:52:56

标签: android xml image-processing layout screenshot

我正在开发Photo Collage App,我遇到了圆形framelayout问题。问题是当我应用圆形 drawable 作为framelayout的背景,然后我获得该布局的截图,我得到了不同的截图(没有圆角)。

here is original layout with round corner

here is screenshot after apply round corner

此功能用于将圆角应用于framelayout

shapeDrawable1 = new GradientDrawable();
        shapeDrawable1.setShape(GradientDrawable.RECTANGLE);
        shapeDrawable1.setCornerRadius(50);
        shapeDrawable1.setColor(getResources().getColor(R.color.transpernt));

  fl_Container_Fragment.setBackground(shapeDrawable1);
        fl_Container_Fragment.setClipToOutline(true);

此功能用于截图:

 private Bitmap getMainFrameBitmap(FrameLayout fl_frag) {
        fl_frag.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(fl_frag.getDrawingCache());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            bitmap.setConfig(Bitmap.Config.ARGB_8888);
        }
        fl_frag.setDrawingCacheEnabled(false);
        Bitmap bmp = bitmap;

        int imgHeight = bmp.getHeight();
        int imgWidth = bmp.getWidth();
        int smallX = 0, largeX = imgWidth, smallY = 0, largeY = imgHeight;
        int left = imgWidth, right = imgWidth, top = imgHeight, bottom = imgHeight;
        for (int i = 0; i < imgWidth; i++) {
            for (int j = 0; j < imgHeight; j++) {
                if (bmp.getPixel(i, j) != Color.TRANSPARENT) {
                    if ((i - smallX) < left) {
                        left = (i - smallX);
                    }
                    if ((largeX - i) < right) {
                        right = (largeX - i);
                    }
                    if ((j - smallY) < top) {
                        top = (j - smallY);
                    }
                    if ((largeY - j) < bottom) {
                        bottom = (largeY - j);
                    }
                }
            }
        }
        Log.d("Trimed bitmap", "left:" + left + " right:" + right + " top:" + top + " bottom:" + bottom);
        return bmp;
    }

请建议我。 感谢

2 个答案:

答案 0 :(得分:0)

我认为此代码对您有用。

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainFrm = (FrameLayout) findViewById(R.id.mainFrm);
        img1 = (ImageView) findViewById(R.id.img1);
        img2 = (ImageView) findViewById(R.id.img2);

        screnshot = (ImageView) findViewById(R.id.img_screenshot);
        btn = (Button) findViewById(R.id.btn_screenshot);
        seek_round = (SeekBar) findViewById(R.id.seek_round);
        bitmap = drawableToBitmap(getResources().getDrawable(R.drawable.pic1));
        seek_round.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                bitmap = getRoundedCornerBitmap(bitmap, i);
                img1.setImageBitmap(bitmap);
                img2.setImageBitmap(bitmap);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        img1.setImageBitmap(getRoundedCornerBitmap(bitmap, 250));
        img2.setImageBitmap(getRoundedCornerBitmap(bitmap, 250));
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                screnshot.setImageBitmap(getMainFrameBitmap(mainFrm));
            }
        });
    }

此函数将drawable转换为位图

public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;

if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    if (bitmapDrawable.getBitmap() != null) {
        return bitmapDrawable.getBitmap();
    }
}

if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
    bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
    bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

使用此函数获取圆角位图:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
        .getHeight(), Bitmap.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(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

xml活动文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lcom56.roundcornerimagedemo.MainActivity">

    <FrameLayout
        android:id="@+id/mainFrm"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/img1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/pic1" />

            <ImageView
                android:id="@+id/img2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/pic1" />
        </LinearLayout>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:src="@mipmap/ic_launcher" />
    </FrameLayout>

    <SeekBar
        android:id="@+id/seek_round"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="1000" />

    <Button
        android:id="@+id/btn_screenshot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img"
        android:layout_gravity="center"

        android:text="Take Screenshot" />

    <ImageView
        android:id="@+id/img_screenshot"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_below="@+id/btn_screenshot"
        android:layout_weight="1" />
</LinearLayout>

here is Output Sample

答案 1 :(得分:0)

这是第二个解决方案。

您可以使用圆角Framelayout(自定义Framelayout)

RoundCornerFramelayout.java

MinHeight

活动的OnCreate方法

FlyoutPresenter

活动的文件

package com.example.pc.roundcornerimagedemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.widget.FrameLayout;
import android.widget.ImageView;


public class RoundCornerFramelayout extends FrameLayout {
    private float CORNER_RADIUS = 50.0f;

    private Bitmap maskBitmap;
    private Paint paint, maskPaint;
    private float cornerRadius;
    private float oldRadius;
    private boolean isFirstTime = true;

    public RoundCornerFramelayout(Context context) {
        super(context);
        init(context, null, 0);
    }

    public RoundCornerFramelayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public RoundCornerFramelayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    public void setCORNER_RADIUS(float CORNER_RADIUS) {
        this.CORNER_RADIUS = CORNER_RADIUS;
        requestLayout();

    }

    private void init(Context context, AttributeSet attrs, int defStyle) {

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        setWillNotDraw(false);
    }

    private static final String TAG = "RoundImageView";

    @Override
    public void draw(Canvas canvas) {
        Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas offscreenCanvas = new Canvas(offscreenBitmap);
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        Log.i(TAG, "draw: " + CORNER_RADIUS);
        cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
        Log.i(TAG, "draw cornerRadius: " + cornerRadius);
        super.draw(offscreenCanvas);

        if (oldRadius != cornerRadius || isFirstTime) {
            maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
            isFirstTime = false;
            oldRadius = cornerRadius;
        }

        offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
        canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);
    }

    private Bitmap createMask(int width, int height) {
        Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
        Canvas canvas = new Canvas(mask);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);

        canvas.drawRect(0, 0, width, height, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        Log.i(TAG, "createMask: cornerRadius: " + cornerRadius);
        canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);

        return mask;
    }
}