Android剪辑自定义视图到路径

时间:2016-03-02 17:16:23

标签: java android view mask

我正在尝试将一个自定义视图(linearlayout)剪辑到另一个(简单视图)。我想将线性布局的路径剪切为视图的路径,即圆形。以下是我的自定义视图:

package classes;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class Circle extends View {

private Paint paint = new Paint();
private Path path = new Path();

public Circle(Context context) {
    super(context);
}

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

public Circle(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setColor(int color) {
    paint.setColor(color);
    invalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    path = new Path();
    path.addCircle(getWidth() / 2 - getWidth() / 5, getHeight() / 2 - getHeight() / 5, (getWidth() + getHeight()) / 4, Path.Direction.CW);
    path.close();
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawPath(path, paint);
}

public Path getPath() {
    return path;
}

}

线性布局:

package classes;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class ClippedLinearLayout extends LinearLayout {

public ClippedLinearLayout(Context context) {
    super(context);
    this.setWillNotCacheDrawing(false);
    invalidate();
}

public ClippedLinearLayout(Context context, AttributeSet attrs, int     defStyle) {
    super(context, attrs, defStyle);
    this.setWillNotCacheDrawing(false);
    invalidate();
}

public ClippedLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setWillNotCacheDrawing(false);
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.clipPath(Blog.circle2.getPath());
}
}

我做错了什么?

编辑:

我的xml:

<RelativeLayout
        android:id="@+id/menuButtonContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">

        <classes.Circle
            android:id="@+id/circle2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="gone" />

        <classes.Circle
            android:id="@+id/menuCircle"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:visibility="gone" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:background="@drawable/menu_button" />

    </RelativeLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/close" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:background="#FF0000"
        android:contentDescription="@string/ContentDescription"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/failLogin"
        android:textColor="#FFFFFF" />

    <ScrollView
        android:id="@+id/menuScrollView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:clickable="false">

        <classes.ClippedLinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:visibility="gone"
            android:orientation="vertical" />

    </ScrollView>

0 个答案:

没有答案