ViewPager的圆形

时间:2015-10-09 12:43:16

标签: android canvas ondraw

嗨我有一个带有viewpager的相对布局。

<RelativeLayout
    android:id="@+id/bubble_circle"
    android:layout_width="200px"
    android:layout_height="200px"
    android:layout_centerHorizontal="true"
    android:background="@drawable/circle">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:overScrollMode="never"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"/>
</RelativeLayout>>

我希望此视图寻呼机具有圆形形状。 我试图覆盖relativelayout的方法dispachdraw

paint = new Paint();
mShadowBounds.top = 0;
mShadowBounds.bottom = canvas.getHeight();
mShadowBounds.left = 0;
mShadowBounds.right = canvas.getWidth();

paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawOval(mShadowBounds, paint);

但它只在顶部涂上一个黑色圆圈。 我该怎么办?

由于

2 个答案:

答案 0 :(得分:1)

在xml中使用以下类作为父级,

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

public class RoundedCornerLayout extends FrameLayout
{
    private final static float CORNER_RADIUS = 100.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)
    {
        pain_border = new Paint();
        pain_border.setFlags(Paint.ANTI_ALIAS_FLAG);
        pain_border.setColor(Color.WHITE);
        pain_border.setStyle(android.graphics.Paint.Style.STROKE);
        pain_border.setStrokeWidth(6);

        paint_inside_background = new Paint();
        paint_inside_background.setColor(Color.parseColor("#686868"));

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

    Paint pain_border;
    Paint paint_inside_background;

    @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());
        }
        canvas.drawCircle(maskBitmap.getWidth() / 2, maskBitmap.getHeight() / 2, maskBitmap.getWidth() / 2, paint_inside_background);

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


        canvas.drawCircle((maskBitmap.getWidth() / 2), (maskBitmap.getHeight() / 2), (maskBitmap.getWidth() - 6) / 2, pain_border);
    }

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

确保您必须使用 dp 而不是 px 来提高性能。

答案 1 :(得分:-1)

试试这个:

在drawable文件夹中创建一个文件oval.xml。

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval">
<solid android:color="@color/transparent" />
</shape>

使用此drawable作为视图的背景:

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:overScrollMode="never"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:drawable/oval"/>