0

I am using a custom layout for having rounded corners.Please find the code for the custom layout.

public class RoundedCornerLayout extends FrameLayout {
    private final static float CORNER_RADIUS = 20.0f;

    private Bitmap maskBitmap;
    private Paint paint, maskPaint;
    private float cornerRadius;

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

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

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

    private void init(Context context, AttributeSet attrs, int defStyle) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);

        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);
    }

    @Override
    public void draw(Canvas canvas) {
        Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas offscreenCanvas = new Canvas(offscreenBitmap);

        super.draw(offscreenCanvas);

        if (maskBitmap == null) {
            maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
        }

        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));
        canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);

        return mask;
    }
}

I have included a ListView inside this layout. But the ListView is not scrolling when added inside this layout. But if I change the layout to a FrameLayout the ListView scroll will work.

The XML layout I am using,

<com.mob.utils.RoundedCornerLayout 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"
    ">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#E8E9E8">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="60dp"
            android:background="#F9B541">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Select Recepients"
                android:textColor="#ffffff"
                android:textSize="8pt"
                android:id="@+id/textView9"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center">
            <info.hoang8f.android.segmented.SegmentedGroup
                android:id="@+id/friendSegmented"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="horizontal"
                android:layout_centerInParent="true">

                <RadioButton
                    android:id="@+id/rdBtn_Facebook"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Facebook"
                    style="@style/RadioButton" />

                <RadioButton
                    android:id="@+id/rdBtn_Email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Email"
                    style="@style/RadioButton" />
            </info.hoang8f.android.segmented.SegmentedGroup>
            </RelativeLayout>

        <LinearLayout
            android:id="@+id/recepientContainer"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></LinearLayout>

    </LinearLayout>
</com.mob.utils.RoundedCornerLayout>

How to fix this?

Thanks

Zach
  • 9,989
  • 19
  • 70
  • 107
  • 1
    paste your layout xml – Juanjo Vega Feb 24 '15 at 00:22
  • just add log statment in draw and check if it is called if you scroll the ListView i'm prety sure that ListView is not invalidate its parent so it is not called ... anyway what you wana achive?(image) maybe it is possible with background and some padding ... – Selvin Feb 24 '15 at 00:27
  • ... isn't it somthing like CardView from support library? – Selvin Feb 24 '15 at 00:33
  • @Selvin I am trying to display all contacts in a rounded corner layout and I want to be able to scroll the listview to display all contacts. I am dynamically replacing recepientContainer using fragment transaction to display all contacts. – Zach Feb 24 '15 at 00:37
  • I don't see the reference to ListView in your posted code. This would be helpful. Additionally, you may want to use an Adapter for virtual memory benefits. – The Original Android Feb 24 '15 at 02:25

0 Answers0