I have in an activity some inputs required to make a drawable shape. This is a little example of it:
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.OVAL);
gd.setColor(Color.RED);
gd.setCornerRadius(12);
preview.getLayoutParams().width = 100;
preview.getLayoutParams().height = 100;
preview.setBackground(gd);
But when I want to implement it into a spinner, I have to choose like all the options multiple times so I can get the result. And also there is a button which should show the preview, doesn't work.
This is my code:
import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import yuku.ambilwarna.AmbilWarnaDialog;
public class CustomizationActivity extends AppCompatActivity {
int currentColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customization);
final EditText widthText = findViewById(R.id.width);
final EditText heightText = findViewById(R.id.height);
final Spinner shapeSpinner = findViewById(R.id.shape);
final View colorView = findViewById(R.id.color);
final View preview = findViewById(R.id.preview);
final EditText size = findViewById(R.id.size);
final EditText corners = findViewById(R.id.corners);
Button button = findViewById(R.id.button);
final GradientDrawable gd = new GradientDrawable();
shapeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
gd.setShape(GradientDrawable.RECTANGLE);
gd.setCornerRadius(Integer.parseInt(corners.getText().toString()));
preview.getLayoutParams().width = Integer.parseInt(widthText.getText().toString());
preview.getLayoutParams().height = Integer.parseInt(heightText.getText().toString());
}
if (position == 2) {
gd.setShape(GradientDrawable.OVAL);
preview.getLayoutParams().width = Integer.parseInt(size.getText().toString());
preview.getLayoutParams().height = Integer.parseInt(size.getText().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
colorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(CustomizationActivity.this, currentColor, false, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
currentColor = color;
colorView.setBackgroundColor(color);
gd.setColor(color);
}
});
dialog.show();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
preview.setBackground(gd);
}
});
}
}
It is somehow working but I don't know how to correct it. What I want is - if it possible - to show a preview directly when a value is changed. - And if not, after editing everything, get the result on button click.
I don't know but I think the problem is with the spinner.
Any help please?
设置属性调用gd.invalidateSelf();
之后,尝试调用onclick()
方法gd.invalidateSelf();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.