大家好,我解释了我的问题,在我的 android 项目中,我在画布上绘制了一个位图。它可以工作,但是当我没有用手指触摸我的画布时,这个位图不显示。我搜索如何解决它。非常感谢你的帮助。
这是我的代码:
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(scaledBm, 0, 0, paint);
canvas.drawPath(path, paint);
if(ModificationActivity.isMask == 1){
Log.e("ondraw", "ok cest un masque");
canvas.drawBitmap(masqueTab[0], 0, 0, paint);
}
}
这是我的完整课程:
This is my complete class
public class DrawingTouchView extends View {
private ArrayList<Draws> drawsList = new ArrayList<Draws>();
private static Bitmap[] masqueTab = new Bitmap[1];
public static Paint paint = new Paint();
public static Path path = new Path();
public Button btnReset;
public LayoutParams params;
private Bitmap bm, scaledBm, mBitmap;
private String imagePath;
private int eraser = 0;
private static int paintOn = 0;
private int resizeOn = 0;
private static int textOn = 0;
private static int maskOn = 0;
private int newX;
private Point origine;
private int minimalWidth = 70;
private Images correspondanceImage = null;
private Texts correspondanceTexte = null;
private static Context mContext;
private static EditText result;
private static int textId = 0;
public DrawingTouchView(Context context, String _imagePath) {
super(context);
mContext = context;
result = new EditText(mContext);
imagePath = _imagePath;
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(15f);
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false;
bfOptions.inPurgeable = true;
bfOptions.inInputShareable = true;
bfOptions.inTempStorage = new byte[32 * 512];
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
if (bm == null) {
File file = new File(imagePath);
FileInputStream fs = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println(e);
}
try {
if (fs != null){
bm = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
}
int nh = (int) (bm.getHeight() * (512.0 / bm.getWidth()));
scaledBm = Bitmap.createScaledBitmap(bm, 512, nh, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(scaledBm, 0, 0, paint);
canvas.drawPath(path, paint);
if(ModificationActivity.isMask == 1){
Log.e("ondraw", "ok cest un masque");
canvas.drawBitmap(masqueTab[0], 0, 0, paint);
}else if(resizeOn == 1){
int newWidth = newX - origine.x;
if(newWidth < minimalWidth){
canvas.drawBitmap(correspondanceImage.scaleImage(minimalWidth), origine.x, origine.y, paint);
}else{
canvas.drawBitmap(correspondanceImage.scaleImage(newWidth), origine.x, origine.y, paint);
}
}else if (textOn == 1){
Texts t = ModificationActivity.textsList.get(0);
TextView finalText = t.getTextView();
ModificationActivity.photo.addView(finalText);
finalText.setOnLongClickListener(new MyTouchListener(1));
finalText.setTag("noCopy");
finalText.setId(t.getId());
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("touch vent", "ok");
float pointX = event.getX();
float pointY = event.getY();
int i = 0;
//mode dessin
if(eraser == 0 && paintOn == 0){
Draws dessin = new Draws(i, Color.GREEN);
drawsList.add(dessin);
paint.setColor(dessin.getColor());
switch (event.getAction()) {
case ACTION_DOWN:
path.moveTo(pointX, pointY);
handleTouch(pointX, pointY);
return true;
case ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
case ACTION_UP:
break;
default:
return false;
}
postInvalidate();
i++;
//mode effaceur
}else if((eraser == 1) && (paintOn == 0)){
Draws effaceur = new Draws(i, Color.YELLOW);
drawsList.add(effaceur);
paint.setColor(effaceur.getColor());
switch (event.getAction()) {
case ACTION_DOWN:
path.moveTo(pointX, pointY);
handleTouch(pointX, pointY);
return true;
case ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
case ACTION_UP:
break;
default:
return false;
}
postInvalidate();
//mode redimenssion d'objet
}else {
if (paintOn == 1) {
switch (event.getAction()) {
case ACTION_DOWN:
path.moveTo(0, 0);
return true;
case ACTION_MOVE:
newX = (int) event.getX();
if (resizeOn == 1) {
correspondanceImage.setBottomRight(new Point((int) event.getX(), (int) event.getY()));
correspondanceImage.setTopRight(new Point((int) event.getX(), correspondanceImage.getTopRight().y));
correspondanceImage.setBottomLeft(new Point((int) correspondanceImage.getBottomLeft().x, (int) event.getY()));
correspondanceImage.setCenter(new Point((int) (correspondanceImage.getTopRight().x + event.getX()) / 2, (int) (correspondanceImage.getTopRight().y + event.getY()) / 2));
}
break;
case ACTION_UP:
for (Images anObjectsList : ModificationActivity.objectsList) {
final ImageView image = anObjectsList.getImage();
final RelativeLayout container = (RelativeLayout) anObjectsList.getContainer();
int childcount = container.getChildCount();
//On affiche l'image sur la photo
container.addView(image);
image.setTag("noCopy");
image.setId(correspondanceImage.getId());
//On definit l'image dragable
image.setOnLongClickListener(new MyTouchListener(0));
//On cherche l'image dropé pour la supprimer
mainloop:
for (int k = 0; k < childcount; k++) {
View v = container.getChildAt(k);
if ((v.getId()) == (correspondanceImage.getId())) {
Log.e("je l'ai!!!!!!!!!!!!!", "ok" + v.getId() + ", corresp =" + correspondanceImage.getId());
ImageView im = (ImageView) container.getChildAt(k);
correspondanceImage.removeImage();
}
}
final ViewGroup.MarginLayoutParams lpt = (ViewGroup.MarginLayoutParams) image.getLayoutParams();
lpt.setMargins(anObjectsList.getOrigine().x, anObjectsList.getOrigine().y, 0, 0);
}
paintOn = 0;
break;
default:
return false;
}
postInvalidate();
}
}
return true;
}
public boolean handleTouch(float pointX, float pointY) {
Point newPoint = new Point();
newPoint.x = (int) pointX;
newPoint.y = (int) pointY;
mainloop:
for (int i = 0; i < ModificationActivity.objectsList.size(); i++) {
correspondanceImage = ModificationActivity.objectsList.get(i);
double distTopRight = Math.sqrt(Math.pow(correspondanceImage.getTopRight().x - newPoint.x, 2) + Math.pow(correspondanceImage.getTopRight().y - newPoint.y, 2));
double distTopLeft = Math.sqrt(Math.pow(correspondanceImage.getTopLeft().x - newPoint.x, 2) + Math.pow(correspondanceImage.getTopLeft().y - newPoint.y, 2));
double distBottomRight = Math.sqrt(Math.pow(correspondanceImage.getBottomRight().x - newPoint.x, 2) + Math.pow(correspondanceImage.getBottomRight().y - newPoint.y, 2));
double distBottomLeft = Math.sqrt(Math.pow(correspondanceImage.getBottomLeft().x - newPoint.x, 2) + Math.pow(correspondanceImage.getBottomLeft().y - newPoint.y, 2));
double distCentre = Math.sqrt(Math.pow(correspondanceImage.getCenter().x - newPoint.x, 2) + Math.pow(correspondanceImage.getCenter().y - newPoint.y, 2));
if(distTopRight <100){
Log.e("en haut a gauche", "on supprime");
}
if (distTopLeft < 100 || distBottomRight < 100 || distBottomLeft < 100 || distCentre < 100) {
Log.e("correspondanceImage","" + correspondanceImage.getId());
origine = correspondanceImage.getOrigine();
paintOn = 1;
resizeOn = 1;
break mainloop;
}
}
if(correspondanceImage == null && !(ModificationActivity.textsList.size() == 0)){
breakloop:
for (int i = 0; i < ModificationActivity.textsList.size(); i++) {
correspondanceTexte = ModificationActivity.textsList.get(i);
double distTopRight = Math.sqrt(Math.pow(correspondanceTexte.getTopRight().x - newPoint.x, 2) + Math.pow(correspondanceTexte.getTopRight().y - newPoint.y, 2));
double distTopLeft = Math.sqrt(Math.pow(correspondanceTexte.getOrigine().x - newPoint.x, 2) + Math.pow(correspondanceTexte.getOrigine().y - newPoint.y, 2));
double distBottomRight = Math.sqrt(Math.pow(correspondanceTexte.getBottomRight().x - newPoint.x, 2) + Math.pow(correspondanceTexte.getBottomRight().y - newPoint.y, 2));
double distBottomLeft = Math.sqrt(Math.pow(correspondanceTexte.getBottomLeft().x - newPoint.x, 2) + Math.pow(correspondanceTexte.getBottomLeft().y - newPoint.y, 2));
double distCentre = Math.sqrt(Math.pow(correspondanceTexte.getCenter().x - newPoint.x, 2) + Math.pow(correspondanceTexte.getCenter().y - newPoint.y, 2));
if(distTopRight <100){
Log.e("en haut a gauche", "on supprime");
}
if (distTopLeft < 100 || distBottomRight < 100 || distBottomLeft < 100 || distCentre < 100) {
Log.e("correspondance texte","" + correspondanceTexte.getId());
origine = correspondanceTexte.getOrigine();
paintOn = 1;
resizeOn = 0;
break breakloop;
}
}
}
return true;
}
public static void newText(){
textOn = 1;
paintOn = 1;
LayoutInflater li = LayoutInflater.from(mContext);
View promptsView = li.inflate(R.layout.dialogedit, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
//set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
result.setText(userInput.getText());
ModificationActivity.addTextToList(new Texts(mContext, textId, Color.BLUE, (result.getText().toString()), 50, new Point(150, 150)));
textId++;
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
//Creation de la boite de dialogue
AlertDialog alertDialog = alertDialogBuilder.create();
//Affichage de la boite de dialogue
alertDialog.show();
}
public static void newMask(Drawable drawable){
Log.e("j'y suis", "ok "+drawable.toString());
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
masqueTab[0] = bitmap;
}
}
这是操纵 iMask 的地方:
public void setMasqueVisibility(boolean visible) {
isMask = 1;
groupBars.setVisibility(View.VISIBLE);
drawBar.setVisibility(View.GONE);
textBar.setVisibility(View.GONE);
objectsBar.setVisibility(View.GONE);
masksBar.setVisibility(View.VISIBLE);
}
public void setDrawVisibility(boolean visible) {
isMask = 0;
groupBars.setVisibility(View.VISIBLE);
textBar.setVisibility(View.GONE);
masksBar.setVisibility(View.GONE);
objectsBar.setVisibility(View.GONE);
drawBar.setVisibility(View.VISIBLE);
}
public void setObjectVisibility(boolean visible) {
isMask = 0;
groupBars.setVisibility(View.VISIBLE);
drawBar.setVisibility(View.GONE);
textBar.setVisibility(View.GONE);
masksBar.setVisibility(View.GONE);
objectsBar.setVisibility(View.VISIBLE);
}
public void setTextVisibility(boolean visible) {
isMask = 0;
groupBars.setVisibility(View.VISIBLE);
drawBar.setVisibility(View.GONE);
textBar.setVisibility(View.VISIBLE);
masksBar.setVisibility(View.GONE);
objectsBar.setVisibility(View.GONE);
}