我想将文本移动到图像上以将文本固定在图像上所需的位置。我成功运行了这段代码,但它不是最佳的,因为它不是用户友好的。
文本移动有时与手指的拾取和放下不匹配。
如果有人有更好的代码请与我分享或让我知道我是否遗漏了什么。
//Listeners for the Canvas that is being awarded
popImgae.setOnTouchListener( new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
someGlobalXvariable = e.getX();
someGlobalYvariable = e.getY();
setTextPosition();
//saveImage(imgRecord.get(1),leftPos,topPos,popText.getTextSize(),popText.getText().toString());
return true;
}
});
public void setTextPosition(){
try {
redrawImage(imgRecord.get(1),Integer.parseInt(imgRecord.get(8)),imgRecord.get(6),Integer.parseInt(imgRecord.get(9)));
} catch (Exception e) {
// TODO: handle exception
System.out.println("##########Error in setTextPositio========="+e.getMessage());
}
}
///Redrawing the image & touchin Move of the Canvas with text
public void redrawImage(String path,float sizeValue,String textValue,int colorValue) {
//Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.fashion_pic);
BitmapFactory.Options options = new BitmapFactory.Options();
try {
options.inMutable = true;
} catch (Exception e) {
// TODO: handle exception
System.out.println("#############Error is======"+e.getMessage());
}
Bitmap bm = BitmapFactory.decodeFile(path,options);
//bm = imageManipulation.convertToMutable(bm);
proxy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(proxy);
//Here, we draw the background image.
c.drawBitmap(bm, new Matrix(), null);
Paint paint = new Paint();
paint.setColor(colorValue); // Text Color
paint.setStrokeWidth(30); // Text Size
paint.setTextSize(sizeValue);
System.out.println("Values passing=========="+someGlobalXvariable+", "+someGlobalYvariable+", "
+sizeValue+", "+textValue);
//Here, we draw the text where the user last touched.
c.drawText(textValue, someGlobalXvariable, someGlobalYvariable, paint);
popImgae.setImageBitmap(proxy);
}