I am trying to draw the lines of a sudoku grid using drawLine() but each line gets progressively more faint, to the point they are not visible about half way down the screen. screenshot of emulator
canvas.drawLine(0, (i + 1) * tileHeight,getWidth(), (i + 1) * tileHeight, gridPaint ); //horizontal lines
I know they are there because for a brief second when the screen is rotated, the lines are displayed as expected but then they disappear again.
I've tried various things to fix this but i still have no idea whats causing this
private static final String TAG = "Soduku";
private final Game game; //game logic
private float tileWidth;
private float tileHeight;
private int selX; //x index of selection
private int selY; //y index of selection
private Rect selRect = new Rect();
private Paint gridPaint = new Paint();
public PuzzleView(Context context) {
super(context);
this.game = (Game)context;
setFocusable(true);
setFocusableInTouchMode(true);
gridPaint.setColor(getResources().getColor(R.color.black));
}
#Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
tileWidth = w / 9f;
tileHeight = h / 9f;
getRect(selX, selY, selRect);
Log.d(TAG, "onSizeChaned: width " + tileWidth + ", height " + tileHeight);
super.onSizeChanged(w, h, oldW, oldH);
}
private void getRect(int x, int y, Rect rect) {
rect.set((int) (x * tileWidth), (int) (y * tileHeight), (int) (x * Math.pow((double) tileWidth, 2)),
(int) (y * Math.pow((double) tileHeight, 2)));
}
#Override
protected void onDraw(Canvas canvas) {
invalidate();
Log.d("onDraw", "tileHeight - " + ((Float) tileHeight).toString());
for(int i = 0; i < 9; i++) {
canvas.drawLine(0, (i + 1) * tileHeight,getWidth(), (i + 1) * tileHeight, gridPaint ); //horizontal lines
canvas.drawLine((i + 1) * tileWidth, 0, (i + 1) * tileWidth, getHeight(), gridPaint); //vertical lines
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown: keyCode " + keyCode + ", keyEvent " + event);
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
select(selX, selY - 1);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
select(selX, selY + 1);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
select(selX - 1, selY);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
select(selX + 1, selY);
break;
case KeyEvent.KEYCODE_0:
case KeyEvent.KEYCODE_SPACE: setSelectedTile(0); break;
case KeyEvent.KEYCODE_1: setSelectedTile(1); break;
case KeyEvent.KEYCODE_2: setSelectedTile(2); break;
case KeyEvent.KEYCODE_3: setSelectedTile(3); break;
case KeyEvent.KEYCODE_4: setSelectedTile(4); break;
case KeyEvent.KEYCODE_5: setSelectedTile(5); break;
case KeyEvent.KEYCODE_6: setSelectedTile(6); break;
case KeyEvent.KEYCODE_7: setSelectedTile(7); break;
case KeyEvent.KEYCODE_8: setSelectedTile(8); break;
case KeyEvent.KEYCODE_9: setSelectedTile(9); break;
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
game.showKeypadOrError(selX, selY);
break;
default:
return super.onKeyDown(keyCode, event);
}
return true;
}
//locate the selected square
private void select(int x, int y) {
invalidate(selRect);
selX = Math.min(Math.max(x,0), 8);
selY = Math.min(Math.max(y,0), 8);
getRect(selX, selY, selRect);
invalidate(selRect);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() != MotionEvent.ACTION_DOWN) {
return super.onTouchEvent(event);
}
select((int)(event.getX() / tileWidth), (int)(event.getY() / tileHeight));
game.showKeypadOrError(selX, selY);
Log.d(TAG, "onTouchEvent: x" + selX + ", y" + selY);
return true;
}
public void setSelectedTile(int tile) {
if(game.setTileIfValid(selX, selY, tile)) {
invalidate();
} else {
//number not valid for selected tile
Log.d(TAG, "setSelectedTile: invalid - " + tile);
startAnimation(AnimationUtils.loadAnimation(game, R.anim.shake));
}
}
Related
public boolean onTouchEvent(MotionEvent event) {
//Log.e(Codistan_Tag, "Touch Event: " + MotionEvent.actionToString(event.getAction()));
if(isImageReady) {
if(isSelectionDragEnabled)
mPanListener.onTouchEvent(event);
if (isZoomEnabled) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
anchor_scale = mScaleFactor;
}
mScaleDetector.onTouchEvent(event);
mPanListener.onTouchEvent(event);
}
else {
switch (event.getAction()) {
case MotionEvent.ACTION_POINTER_3_DOWN:
break;
case MotionEvent.ACTION_POINTER_2_DOWN:
break;
case MotionEvent.ACTION_POINTER_1_UP:
break;
case MotionEvent.ACTION_DOWN:
if (isSelectionToolEnabled)
isDrawFinished = false;
orig_x = event.getX();
orig_y = event.getY();
//Log.e(Codistan_Tag, "-------ORIGIN-------s: " + String.valueOf(orig_x) + " " + String.valueOf(orig_y));
orig_x = (orig_x - (dest.left));
orig_y = (orig_y - (dest.top));
if(java.lang.Math.round(mScaleFactor) > 2) {
orig_x += (scale_cur_x * (riz_scale));
orig_y += (scale_cur_y * (riz_scale));
} else {
orig_x += (scale_cur_x);
orig_y += (scale_cur_y);
}
orig_x /= java.lang.Math.round(mScaleFactor);
orig_y /= java.lang.Math.round(mScaleFactor);
orig_x *= scale;
orig_y *= scale;
//mSelectionTaskManager.setOrigin((int) orig_x, (int) orig_y);
if(isSelectionToolEnabled)
MovementStack.add(new Pair((int)orig_x, (int)orig_y));
Log.e(Codistan_Tag, "Codistan Origins: " + String.valueOf(orig_x) + ", " + String.valueOf(orig_y));
break;
case MotionEvent.ACTION_MOVE:
max_dist = dist * scale;
if (event.getAction() != 1) {
move_x = event.getX();
move_y = event.getY();
//Log.e(Codistan_Tag, "Move: " + String.valueOf(move_x) + ", " + String.valueOf(move_y));
move_x = (move_x - dest.left);
move_y = (move_y - dest.top);
if(java.lang.Math.round(mScaleFactor) > 2) {
move_x += (scale_cur_x * riz_scale);
move_y += (scale_cur_y * riz_scale);
} else {
move_x += (scale_cur_x);
move_y += (scale_cur_y);
}
move_x /= java.lang.Math.round(mScaleFactor);
move_y /= java.lang.Math.round(mScaleFactor);
move_x *= scale;
move_y *= scale;
Log.e(Codistan_Tag, "Codistan Move: " + String.valueOf(move_x) + ", " + String.valueOf(move_y));
if (move_x >= 0 && move_y >= 0) {
if (!isSelectionToolEnabled && isDistortionEnabled) {
warpPhotoFromC(image, height, width, max_dist/mScaleFactor, orig_x, orig_y, move_x, move_y);
first++;
distortedBmp.setPixels(image, 0, width, 0, 0, width, height);
fg = false;
} else {
//Cut Tool Enabled
distortedBmp.setPixels(image, 0, width, 0, 0, width, height);
MovementStack.add(new Pair((int) move_x, (int) move_y));
}
}
}
orig_x = move_x;
orig_y = move_y;
break;
case MotionEvent.ACTION_UP:
if (isSelectionToolEnabled)
isDrawFinished = true;
break;
}
}
v.invalidate();
}
return true;
}
basically m taking the screen shot and applying cropping on it i m selecting the cropping area using finger to draw a portion to crop . when i left moving my finger then the line appear but i want the line to follow my finger
Create a customView over which you can draw the line.As it appears that this view should appear above your image then better extend it with Framelayout with same .And place it above image.
FingerLine.class
public class FingerLine extends FrameLayout {
private final Paint mPaint;
private float startX;
private float startY;
private float endX;
private float endY;
public FingerLine(Context context) {
this(context, null);
}
public FingerLine(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.RED);
}
#Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(startX, startY, endX, endY, mPaint);
}
#Override
public boolean onTouchEvent(#NonNull MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getY();
invalidate();
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
endY = event.getY();
invalidate();
break;
}
return true;
}
}
Note:Don't forget to call invalidate() because it will be responsible to call onDraw
Im tring to drag an imageView (the 4 circles) and for every move event draw lines between the 4 centers of the imageButton.
But every time i drag them the lines are a little bit off the center.
closeLines is an array that contains the 4 imageButtons
private void drawLinesBetweenDots(ImageButton[] closeLines) {
dotHeight=closeLines[0].getHeight()/2;
dotWidth=closeLines[0].getWidth()/2;
canvas.drawLine(closeLines[0].getX()+ dotHeight , closeLines[0].getY() + dotHeight, closeLines[1].getX() + dotHeight, closeLines[1].getY() + dotHeight, paint);
canvas.drawLine(closeLines[0].getX()+ dotHeight , closeLines[0].getY() + dotHeight, closeLines[2].getX() + dotHeight, closeLines[2].getY() + dotHeight, paint);
canvas.drawLine(closeLines[3].getX()+ dotHeight , closeLines[3].getY() + dotHeight, closeLines[1].getX() + dotHeight , closeLines[1].getY() + dotHeight, paint);
canvas.drawLine(closeLines[3].getX()+ dotHeight , closeLines[3].getY() + dotHeight, closeLines[2].getX() + dotHeight , closeLines[2].getY() + dotHeight, paint);
imageView.invalidate();
}
public boolean onTouch(View v, MotionEvent event) {
if(v.getId()== dot1.getId() || v.getId()== dot2.getId() || v.getId()== dot3.getId() || v.getId()== dot4.getId()) {
if (startOrStop) {
float currX, currY;
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mPrevX = event.getX();
mPrevY = event.getY();
isRect = false;
break;
case MotionEvent.ACTION_MOVE:
currX=event.getRawX();
currY=event.getRawY();
/*ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(v.getLayoutParams());
marginParams.setMargins((int) (currX - mPrevX), (int) (currY - mPrevY), 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
v.setLayoutParams(layoutParams);*/
v.setX((int) (currX - mPrevX));
v.setY((int) (currY - mPrevY));
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
closeLines = getCloseLines(v);
//canvas.drawLine((currX - mPrevX),(currY - mPrevY), closeLines[1].getX() + dotHeight, closeLines[1].getY() + dotHeight, paint);
drawLinesBetweenDots(closeLines);
break;
case MotionEvent.ACTION_UP:
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
closeLines = getCloseLines(v);
drawLinesBetweenDots(closeLines);
prevThreshold = null;
isRect = true;
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
}
}
return true;
}
How do i make the lines go from the center of the circles?
I want to draw a line on Imageview.for example when we select a particular text the text get colored under a line.this is my code.I am not able to see the line.and how to increase the width of this line
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
imageView=(ImageView)findViewById(R.id.imageView1);
bitmap = getIntent().getExtras().getParcelable("name");
Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
//bitmap1 = Bitmap.createBitmap((int) dw, (int) dh,Bitmap.Config.ARGB_8888);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.WHITE);
imageView.setImageBitmap(bitmap);
//for co-ordinate of the selected part of the image
imageView.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
x1 = (int) event.getX();
y1 = (int) event.getY();
x2 = (int) event.getX();
y2 = (int) event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
System.out.println("x1-cordinate"+x1+"y1-cordinate"+y1);
//startPt = projectXY((ImageView)v, bitmapMaster, x, y);
xstart=x1;
ystart=y1;
break;
/* case MotionEvent.ACTION_MOVE:
textSource.setText("ACTION_MOVE- " + x + " : " + y);
drawOnRectProjectedBitMap((ImageView)v, bitmapMaster, x, y);
break;*/
case MotionEvent.ACTION_UP:
System.out.println("x2-cordinate"+x2+"y2-cordinate"+y2);
canvas.drawLine(xstart, ystart, x2, y2, paint);
imageView.invalidate();
//drawOnRectProjectedBitMap((ImageView)v, bitmapMaster, x, y);
finalizeDrawing();
break;
}
/*
* Return 'true' to indicate that the event have been consumed.
* If auto-generated 'false', your code can detect ACTION_DOWN only,
* cannot detect ACTION_MOVE and ACTION_UP.
*/
return true;
}});`
I think you want something like this answer.
It will show like below
`
Button btnLoadImage;
TextView textSource, textInfo;
ImageView imageResult;
final int RQS_IMAGE1 = 1;
Uri source;
Bitmap bitmapMaster;
Canvas canvasMaster;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLoadImage = (Button)findViewById(R.id.loadimage);
textSource = (TextView)findViewById(R.id.sourceuri);
imageResult = (ImageView)findViewById(R.id.result);
btnLoadImage.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE1);
}});
imageResult.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
textSource.setText("ACTION_DOWN- " + x + " : " + y);
drawOnProjectedBitMap((ImageView)v, bitmapMaster, x, y);
break;
case MotionEvent.ACTION_MOVE:
textSource.setText("ACTION_MOVE- " + x + " : " + y);
drawOnProjectedBitMap((ImageView)v, bitmapMaster, x, y);
break;
case MotionEvent.ACTION_UP:
textSource.setText("ACTION_UP- " + x + " : " + y);
drawOnProjectedBitMap((ImageView)v, bitmapMaster, x, y);
break;
}
/*
* Return 'true' to indicate that the event have been consumed.
* If auto-generated 'false', your code can detect ACTION_DOWN only,
* cannot detect ACTION_MOVE and ACTION_UP.
*/
return true;
}});
}
/*
* Project position on ImageView to position on Bitmap
* draw on it
*/
private void drawOnProjectedBitMap(ImageView iv, Bitmap bm, int x, int y){
if(x<0 || y<0 || x > iv.getWidth() || y > iv.getHeight()){
//outside ImageView
return;
}else{
int projectedX = (int)((double)x * ((double)bm.getWidth()/(double)iv.getWidth()));
int projectedY = (int)((double)y * ((double)bm.getHeight()/(double)iv.getHeight()));
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
canvasMaster.drawCircle(projectedX, projectedY, 5, paint);
imageResult.invalidate();
textSource.setText(x + ":" + y + "/" + iv.getWidth() + " : " + iv.getHeight() + "\n" +
projectedX + " : " + projectedY + "/" + bm.getWidth() + " : " + bm.getHeight()
);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap tempBitmap;
if(resultCode == RESULT_OK){
switch (requestCode){
case RQS_IMAGE1:
source = data.getData();
textSource.setText(source.toString());
try {
//tempBitmap is Immutable bitmap,
//cannot be passed to Canvas constructor
tempBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(source));
Config config;
if(tempBitmap.getConfig() != null){
config = tempBitmap.getConfig();
}else{
config = Config.ARGB_8888;
}
//bitmapMaster is Mutable bitmap
bitmapMaster = Bitmap.createBitmap(
tempBitmap.getWidth(),
tempBitmap.getHeight(),
config);
canvasMaster = new Canvas(bitmapMaster);
canvasMaster.drawBitmap(tempBitmap, 0, 0, null);
imageResult.setImageBitmap(bitmapMaster);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
}`free draw on bitmap followed this link and now I am able to draw the line above the ImageView
I am making crossword app i want to set my text horizontal & vertical both now its coming in cross align i want to make it vertical android another text to vertical
public class PuzzleView extends View {
private float width; // width of one tile
private float height; // height of one tile
private int selX; // X index of selection
private int selY; // Y index of selection
private final Rect selRect = new Rect();
private final Game game;
public PuzzleView(Context context) {
super(context);
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
width = w / 9f;
height = h / 9f;
getRect(selX, selY, selRect);
Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
super.onSizeChanged(w, h, oldw, oldh);
}
#Override
protected void onDraw(Canvas canvas) {
// Draw the background...
Paint background = new Paint();
background.setColor(getResources().getColor(R.color.puzzle_background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
// Draw the board...
// Define colors for the grid lines
Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.puzzle_dark));
Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
Paint light = new Paint();
light.setColor(getResources().getColor(R.color.puzzle_light));
// Draw the minor grid lines
for (int i = 0; i < 9; i++) {
canvas.drawLine(0, i * height, getWidth(), i * height, light);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), light);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
// Draw the major grid lines
for (int i = 0; i < 9; i++) {
if (i % 3 != 0)
continue;
canvas.drawLine(0, i * height, getWidth(), i * height, dark);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
// Draw the numbers...
// Define color and style for numbers
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
foreground.setStyle(Style.FILL);
foreground.setTextSize(height * 0.75f);
foreground.setTextScaleX(width / height);
foreground.setTextAlign(Paint.Align.CENTER);
// // Draw the number in the center of the tile
FontMetrics fm = foreground.getFontMetrics();
// // Centering in X: use alignment (and X at midpoint)
float x = width / 2;
// // Centering in Y: measure ascent/descent first
float y = height / 2 - (fm.ascent + fm.descent) / 2;
Paint hint = new Paint();
String str = "TEXT";
int m;
for (m = 0; m < str.length(); m++) {
System.out.println(str.charAt(m));
char convertst = str.charAt(m);
String characterToString = Character.toString(convertst);
//canvas.drawText(characterToString, x, y, hint);
canvas.drawText(characterToString, m
* width + x, m * height + y, foreground); //its working in cross
hint.setColor(Color.BLACK);
hint.setTextSize(45);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN)
return super.onTouchEvent(event);
select((int) (event.getX() / width), (int) (event.getY() / height));
game.showKeypadOrError(selX, selY);
Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown: keycode=" + keyCode + ", event=" + event);
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
select(selX, selY - 1);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
select(selX, selY + 1);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
select(selX - 1, selY);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
select(selX + 1, selY);
break;
case KeyEvent.KEYCODE_0:
case KeyEvent.KEYCODE_SPACE:
setSelectedTile(0);
break;
case KeyEvent.KEYCODE_1:
setSelectedTile(1);
break;
case KeyEvent.KEYCODE_2:
setSelectedTile(2);
break;
case KeyEvent.KEYCODE_3:
setSelectedTile(3);
break;
case KeyEvent.KEYCODE_4:
setSelectedTile(4);
break;
case KeyEvent.KEYCODE_5:
setSelectedTile(5);
break;
case KeyEvent.KEYCODE_6:
setSelectedTile(6);
break;
case KeyEvent.KEYCODE_7:
setSelectedTile(7);
break;
case KeyEvent.KEYCODE_8:
setSelectedTile(8);
break;
case KeyEvent.KEYCODE_9:
setSelectedTile(9);
break;
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
game.showKeypadOrError(selX, selY);
break;
default:
return super.onKeyDown(keyCode, event);
}
return true;
}
public void setSelectedTile(int tile) {
if (game.setTileIfValid(selX, selY, tile)) {
invalidate();// may change hints
} else {
// Number is not valid for this tile
Log.d(TAG, "setSelectedTile: invalid: " + tile);
startAnimation(AnimationUtils.loadAnimation(game, R.anim.shake));
}
}
private void select(int x, int y) {
invalidate(selRect);
selX = Math.min(Math.max(x, 0), 8);
selY = Math.min(Math.max(y, 0), 8);
getRect(selX, selY, selRect);
invalidate(selRect);
}
private void getRect(int x, int y, Rect rect) {
rect.set((int) (x * width), (int) (y * height),
(int) (x * width + width), (int) (y * height + height));
}
}
///////////////////////////////////////////////////
some part of the code enter code here
width = w / 9f;
height = h / 9f;
float x = width / 2;
// // Centering in Y: measure ascent/descent first
float y = height / 2 - (fm.ascent + fm.descent) / 2;
/// This is the main area where i am handling the position /////
String str = "TEXT";
int m;
for (m = 0; m < str.length(); m++) {
System.out.println(str.charAt(m));
char convertst = str.charAt(m);
String characterToString = Character.toString(convertst);
canvas.drawText(characterToString, m
* width + x, m * height + y, foreground); //its working in cross
hint.setColor(Color.BLACK);
hint.setTextSize(45);
}
here is the view i am getting:
Here is the view i which i want :
Do not increase start parameter.
canvas.drawText(characterToString, x, m * height + y, foreground);
I need to make a arrow head in a line that is defined by a lot of points, that the user will drawn.
I hope that I could be clear about what is my question.
Thanks.
Note: All the questions/answers that I saw here were to resolve the problem of a line that was defined by two points, taken in the ACTION_DOWN and ACTION_UP. It's different in my case, because I need to take the first point while the line is being drawn.
This is my onTouch() method. It just draw a line defined by where the user touches in the screen.
public boolean onTouch(View v, MotionEvent event) {
if(getEditMode()) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
//Here i have to draw an arrow.
drawCanvas.drawPath(path, paint);
path.reset();
invalidate();
break;
}
invalidate();
return true;
} else {
return false;
}
}
I override draw and rotate the canvas before drawing the arrow.
//NOTE: 0, 0 is the bottom center point
vehicle.draw(canvas);
//draw arrow
if (arrow != null && heading != BusLocation.NO_HEADING)
{
//put the arrow in window
int arrowLeft = -(arrow.getIntrinsicWidth() / 4);
int arrowTop = -vehicle.getIntrinsicHeight() + arrowTopDiff;
//use integer division when possible. This code is frequently executed
int arrowWidth = (arrow.getIntrinsicWidth() * 6) / 10;
int arrowHeight = (arrow.getIntrinsicHeight() * 6) / 10;
int arrowRight = arrowLeft + arrowWidth;
int arrowBottom = arrowTop + arrowHeight;
arrow.setBounds(arrowLeft, arrowTop, arrowRight, arrowBottom);
canvas.save();
//set rotation pivot at the center of the arrow image
canvas.rotate(heading, arrowLeft + arrowWidth/2, arrowTop + arrowHeight / 2);
Rect rect = arrow.getBounds();
arrow.draw(canvas);
arrow.setBounds(rect);
canvas.restore();
}
}
So, I found a solution.
I store all the points that construct the line in an arraylist.
After I took a point of the line that would be in the 0.9 * arraylist.size() as reference to draw an arrowhead that follow the line direction.
Here is my onTouch() method and the funciont that I used to draw the arrow.
public boolean onTouch(View v, MotionEvent event) {
if(getEditMode()) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
path.moveTo(eventX, eventY);
pList.clear(); // When I start a line need to clear the previous coorfinates.
break;
}
case MotionEvent.ACTION_MOVE: {
int hSize = event.getHistorySize();
path.lineTo(eventX, eventY);
PointF aux, auxH;
if(darrow) {
if(hSize > 0) { // If movement is too fast.
for(int i = 0; i < hSize; i++) {
auxH = new PointF(event.getHistoricalX(i), event.getHistoricalY(i));
pList.add(auxH);
}
}
aux = new PointF(eventX, eventY);
pList.add(aux);
}
break;
}
case MotionEvent.ACTION_UP: {
pend.x = eventX;
pend.y = eventY;
if(darrow) { // If need to draw an arrow head to the end of the line;
arrowPath = drawArrow(pend); // Store the right arrowhead to a path.
}
drawCanvas.drawPath(path, paint);
drawCanvas.drawPath(arrowPath, paint);
path.reset();
arrowPath.reset();
invalidate();
break;
}
}
invalidate();
return true;
} else {
return false;
}
}
And the drawArrow(PointF) function:
private Path drawArrow(PointF pfinal) {
float dx, dy;
PointF p1, p2;
PointF pstart;
Path auxPath = new Path();
if(pList.size() > 0) {
PointF[] auxArray = pList.toArray(new PointF[pList.size()]);
int index = (int)(auxArray.length * 0.9);
Log.d(msg, "Size: " + auxArray.length + " | index: " + index);
pstart = auxArray[index];
dx = pfinal.x - pstart.x;
dy = pfinal.y - pstart.y;
float length = (float)Math.sqrt(dx * dx + dy * dy);
float unitDx = dx / length;
float unitDy = dy / length;
final int arrowSize = 10;
p1 = new PointF(
(float)(pfinal.x - unitDx * arrowSize - unitDy * arrowSize),
(float)(pfinal.y - unitDy * arrowSize + unitDx * arrowSize));
p2 = new PointF(
(float)(pfinal.x - unitDx * arrowSize + unitDy * arrowSize),
(float)(pfinal.y - unitDy * arrowSize - unitDx * arrowSize));
auxPath.moveTo(pfinal.x, pfinal.y);
auxPath.lineTo(p1.x, p1.y);
auxPath.moveTo(pfinal.x, pfinal.y);
auxPath.lineTo(p2.x, p2.y);
auxPath.close();
}
return auxPath;
}