レンダリングではないカスタムのViewGroup内部の変更点

モハマド・Hosein Kalantarian:

私は別のクラスにインサイドハンドルビューにカスタムのViewGroupを作ったが、私は値が変化内のTextViewの値を変更するが、のCustomViewではないショーをした場合、子供のビューに加えられた変更はいずれも、たとえば、表示されません。

私は、カスタムビューを作成するために、Googleのガイドに従い、コーナーが放射状にするコードを追加しています。私は、ログにテストされ、値は問題はビューがそれらを表示されていないされ、バックグラウンドで変化しています!

私のコード:


public class ShoppingButton extends FrameLayout {
    private final static float CORNER_RADIUS = 16.0f;

    private TextView plusBtn, minusBtn, valueTv;
    private ImageView overlayIv;

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

    private int minValue = 0, maxValue = Integer.MAX_VALUE;

    public ShoppingButton(Context context) {
        super(context);
        init(context);
    }

    public ShoppingButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShoppingButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View rootView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.shopping_button, this);
        plusBtn = rootView.findViewById(R.id.plus_btn);
        minusBtn = rootView.findViewById(R.id.minus_btn);
        overlayIv = rootView.findViewById(R.id.overlay_iv);
        valueTv = rootView.findViewById(R.id.value_tv);
        overlayIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                overlayIv.setVisibility(GONE);
                setValue(1);
            }
        });
        plusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("CLICK", "PLUS");
                incrementValue();
            }
        });
        minusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("CLICK", "MINUS");
                decrementValue();
            }
        });

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

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getValue() {
        return Integer.valueOf(valueTv.getText().toString());
    }

    public void setValue(int value) {
        Log.e("VALUE", "=" + value);
        if (value > maxValue)
            return;

        if (value == minValue + 1)
            minusBtn.setText("x");
        else
            minusBtn.setText("-");

        if (value == minValue)
            overlayIv.setVisibility(VISIBLE);

        plusBtn.setEnabled(!(value == maxValue));

        valueTv.setText(String.valueOf(value));
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    private void incrementValue() {
        int currentValue = Integer.valueOf(valueTv.getText().toString());
        if (currentValue < maxValue)
            setValue(currentValue + 1);
    }

    private void decrementValue() {
        int currentValue = Integer.valueOf(valueTv.getText().toString());
        if (currentValue > minValue)
            setValue(currentValue - 1);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="@dimen/_36sdp"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true">

    <ImageView
        android:id="@+id/overlay_iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/secondary"
        android:padding="8dp"
        android:visibility="gone"
        app:srcCompat="@drawable/ic_add_shopping_cart" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/secondary"
        android:orientation="vertical">

        <TextView
            android:id="@+id/plus_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/secondary_variant"
            android:gravity="center"
            android:padding="4dp"
            android:text="+"
            android:textColor="@color/surface_light"
            android:textSize="@dimen/_16ssp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/value_tv"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/secondary"
            android:gravity="center"
            android:text="0"
            android:textColor="@color/surface_light"
            android:textSize="@dimen/_14ssp" />

        <TextView
            android:id="@+id/minus_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/secondary_variant"
            android:gravity="center"
            android:padding="4dp"
            android:text="-"
            android:textColor="@color/surface_light"
            android:textSize="@dimen/_16ssp"
            android:textStyle="bold" />
    </LinearLayout>

</FrameLayout>
ハミドkeyhani:

変更したり、コメントや、この行を削除します。

setWillNotDraw(false);

このアウトをチェック- > setwillnotdrawの行動(偽)

私は有用であると願っています。)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=364952&siteId=1