base on the this app : https://github.com/valerio-bozzolan/AcrylicPaint/blob/master/src/anupam/acrylic/EasyPaint.java
i want to add another item in the menu, which is drawing a line. I'm new to android, so please don't judge me :) here's my attempt:
public boolean onTouch(View v, MotionEvent event) {
float downx = 0, downy = 0, upx = 0, upy = 0;
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
mCanvas.drawLine(downx, downy, upx, upy, mPaint); //<---
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
And the i add this lines in onOptionsItemSelected (line 314)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.LINE_ID:
//i need to call it here
return true;
thank you!!
Related
I have drag and drop imageView(multiple imageView with same object) in layout and after that ImageView onTouch it move around layout. but after move imageView it overlapping with each other, how do prevent overalapping image?
also every image object is same. here is my code :
public boolean onDrag(View view, DragEvent event) {
recyclerViewFloorTiles = findViewById(R.id.rvfloortiles);
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENDED:
break;
case DragEvent.ACTION_DROP:
dropX = event.getX();
dropY = event.getY();
final ObjectAdapter.DragData state = (ObjectAdapter.DragData) event.getLocalState();
final ImageView shape = (ImageView) LayoutInflater.from(this).inflate(
R.layout.layout_drag_image, dropContainer, false);
shape.setImageResource(state.item.getDrawableRes());
shape.setX(dropX - (float) state.width / 2);
shape.setY(dropY - (float) state.height / 2);
shape.getLayoutParams().width = state.width;
shape.getLayoutParams().height = state.height;
dropContainer.addView(shape);
shape.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
ImageView objectDropImage = new ImageView(DashboardActivity.this);
objectDropImage.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
dropContainer.addView(objectDropImage);
float newX, newY;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
newX = event.getRawX() + dX;
newY = event.getRawY() + dY;
if ((newX <= 0 || newX >= screenWidth-view.getWidth()) || (newY <= 0 || newY >= screenHight-view.getHeight()))
{
break;
}
view.setX(newX);
view.setY(newY);
break;
}
return false;
}
});
}
break;
default:
break;
}
return true;
}
I have a library which handles onTouchListener Events for a ImageView.
view.setOnTouchListener(new View.OnTouchListener() {
int lastAction;
float dX = 0;
float dY = 0;
int DELTA = 50;
private static final int MAX_CLICK_DURATION = 200;
private long pressStartTime;
#Override
public boolean onTouch(View view, final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
pressStartTime = System.currentTimeMillis();
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN) {
break;
}
break;
case MotionEvent.ACTION_MOVE:
lastAction = MotionEvent.ACTION_MOVE;
break;
}
return false;
}
});
And then once installing the library into my app i also wanted to set my own onTouchEvent for that ImageView.
view.setOnTouchListener(new new View.OnTouchListener(){});
The problem is that only the library touchListener event gets called and the app one is lost/neglected. If I remove the library then the app touch event gets called. How can I make it so that both the library and the app touchListeners are called?
I have got this simple code to rotate an image with onTouchListener, but it seems to be zooming in and out on the image as well. I am really new to using onTouchListener. Could someone tell me what is happening, or how to fix this. Anything helps thanks:)
public class MainActivity extends Activity implements OnTouchListener {
private ImageView dialer;
private float y=0;
public boolean onTouch(View v, MotionEvent event) {
double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
//x=event.getX();
y=event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}//onTouch
private void updateRotation(double rot){
float newRot= new Float(rot);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.round_button_big);
Matrix matrix= new Matrix();
matrix.postRotate(newRot,bitmap.getWidth()/2,bitmap.getHeight()/2);
if(y>250){
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
else{
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialer = (ImageView) findViewById(R.id.image);
dialer.setOnTouchListener(this);
}//onCreate
}
Try to use this code on touch event:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// reset the touched quadrants
for (int i = 0; i < quadrantTouched.length; i++) {
quadrantTouched[i] = false;
}
allowRotating = false;
startAngle = getAngle(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
double currentAngle = getAngle(event.getX(), event.getY());
rotateDialer((float) (startAngle - currentAngle));
startAngle = currentAngle;
break;
case MotionEvent.ACTION_UP:
allowRotating = true;
break;
}
// set the touched quadrant to true
quadrantTouched[getQuadrant(event.getX() - (dialerWidth / 2), dialerHeight - event.getY() - (dialerHeight / 2))] = true;
detector.onTouchEvent(event);
return true;
And here you can find an example :http://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868
Hey Friends help me to achieve the objective. I am newbie to the android development and the only thing I want to do is that I am trying to drag a button and drop it onto the other View(i.e RelativeLayout) the very first thing I have done the code for the drag.
rootView = inflater.inflate(R.layout.fragment_game_map,container,false);
button1 = (Button) rootView.findViewById(R.id.button1);
button1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(button1);
v.startDrag(dragData,myShadow,null,0);
return true;
}
});
button1.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENDED");
// Do nothing
break;
case DragEvent.ACTION_DROP:
Log.d(TAG, "ACTION_DROP event");
// Do nothing
break;
default:
break;
}
return true;
}
});
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(button1);
button1.startDrag(data, shadowBuilder, button1, 0);
button1.setVisibility(View.INVISIBLE);
return true;
}
else
{
return false;
}
}
});
return rootView;
}
but the second thing I don't have Idea to do.. that is I want to drop it onto the RelativeLayout but the RelativeLayout can only allow one button to be drop on it.
You missing the point of DragLiatenr. DragListenr listen to event of dragging, and handle them properly, so if your RelativeLayout not register to DragEvent he can't handle them.
Actually for what you asking you just need to listen for ACTION_DROP, and return true for all the others events.
Your could should like something like:
final RelativeLayout container; //You need to initialize your Relativelayout here
container.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
Log.d(TAG, "ACTION_DROP event");
((ViewGroup)v.getParent()).removeView(v); //you must first remove view from it former container
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
container.addView(v, params); // of course you need to modify params for your need
return true;
break;
default:
return true;
}
}
});
Also why you put onTouchListenr you never use?
I have a button for which I am using onTouch so that I can move it around. But the button is now failing to load the contents in onClick. How to make it happen ?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//some code which dosent work
}
#Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
Voice v1 = new Voice();
v1.putVoice();
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return true;
}
return false;
}
I finally found out that if we differentiate with ACITON_MOVE position we can get a click event.
Here is the snippet which did the trick -
case MotionEvent.ACTION_UP:
if(Math.abs(event.getRawX()-initialTouchX )<=2){
floatingFaceBubble.performClick();
return false;
}