Mohammad Hosein Kalantarian:
Hice un ViewGroup costumbre de vistas manija dentro de una clase diferente, pero ninguno de los cambios realizados en las opiniones de los niños están mostrando, por ejemplo, si cambio de un valor TextView dentro de los cambios de valor, pero no lo hace shows en el CustomView.
Estoy siguiente guía de Google para hacer vistas personalizadas y añadió un código para hacer esquinas radial. Probé en el registro y los valores están cambiando en el fondo del problema es que la vista no está mostrando ellos!
Mi código:
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>
Hamid Keyhani:
cambiar o comentario o eliminar esta línea:
setWillNotDraw(false);
mira esto -> Comportamiento de setwillnotdraw (falso)
Espero que sea útil;)