I've tried many different solutions and none worked.
So, how do I drag and drop a Bitmap?
Here is a possible solution that also doesn't work, but I would love to know why:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.our_image);
And then activate the OnTouchListener:
#Override
public boolean onTouch (View v, MotionEvent event)
{
Imageview iv = new ImageView(this);
iv.setImageBitmap(bm);
if (v == iv)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
eventX = event.getXPrecision();
eventY = event.getYPrecision();
break;
case MotionEvent.ACTION_MOVE:
distX = event.getX() - eventX;
distY = event.getY() - eventY;
iv.setX(distX + eventX);
iv.setY(distY + eventY);
eventX = event.getXPrecision();
eventY = event.getYPrecision();
}
return true;
}
return false;
}
and when I move the ImageView, I update the Bitmap's location to it's coordinates
by creating a canvas and redrawing the Bitmap:
Paint p = new Paint()
Canvas canvas = new Canvas(bm);
canvas.drawBitmap(bm, iv.getLeft(), iv.getTop(), p);
Do I need to put invalidate() here? And if so, why?
Any kind of help would be greatly appreciated! ^_^
Related
I'm trying to draw over an image, which i'll save later with the drawings. My xml only have the ImageView and I tried to overwrite onDraw. It only made de Canvas works, but with no image.
public class Body1 extends View {
public LayoutParams params;
private Path path = new Path();
private Paint brush = new Paint();
public Body1(Context context) {
super(context);
brush.setAntiAlias(true);
brush.setColor(Color.MAGENTA);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(8f);
params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
default:
return false;
}
postInvalidate();
return false;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, brush);
}
}
OK you are on the right path.
What you are missing is invalidate calls that will result in the OS calling the OnDraw of the view.
Call invalidate every time you want to update the view. But notice that your class has only a single path object and so will only draw the last segment.
If I'm not mistaken you should hold an ArrayList of Path objects.
In onDraw you should loop over them and draw them all one after the other.
I want to move any normal view in my app along with swipe.
I am able to detect swipe using OnTouchListener in onTouch method. But am not able to move the view along with the swipe.
I want to move the view along with the swipe.
I think so that something i need to implement inside onTouch method to move the view or item along with swipe, but what i don't know.
Any help will be highly appreciated.
You just need to use view.setTranslationX(diffX) for X coordinate and view.setTranslationY(diffY) for Y coordinate while its MotionEvent.ACTION_MOVE under onTouch() method.
view.setOnTouchListener(new View.OnTouchListener() {
float downX, downY, nowX, nowY, diffX, diffY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getRawX();
downY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
nowX = event.getRawX();
nowY = event.getRawY();
diffX = nowX - downX;
diffY = nowY - downY;
v.setTranslationX(diffX);
v.setTranslationY(diffY);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
How is Skype managing to show video call drag able pop up on home screen after minimizing Skype app during call. You can select and start other application from home screen and Skype popup is still on screen even parallel with other application. If it is App widget then how do I add or remove on home screen and in top of other application UI.
Use WindowManager to add a Float view:
private void createFloatView()
{
Button btn_floatView = new Button();
btn_floatView = new Button(getApplicationContext());
btn_floatView.setText("FloatView");
wm = (WindowManager) getApplicationContext().getSystemService(
Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
params.format = PixelFormat.RGBA_8888;
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = 100;
params.height = 100;
btn_floatView.setOnTouchListener(new OnTouchListener()
{
int lastX, lastY;
int paramX, paramY;
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
paramX = params.x;
paramY = params.y;
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
params.x = paramX + dx;
params.y = paramY + dy;
wm.updateViewLayout(btn_floatView, params);
break;
}
return true;
}
});
wm.addView(btn_floatView, params);
isAdded = true;
}
Is there any way to disable getView() method in ListView Adapter when I use MotionEvent.ACTION_MOVE in OnTouchListener??
I need to do because i try move my conteiner(inside is my ListView) by finger. I use for this method setLayoutParams:
private class MyListener implements View.OnTouchListener{
#Override
public boolean onTouch(View v, MotionEvent event) {
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
RelativeLayout.LayoutParams paramsUp = (RelativeLayout.LayoutParams) myBar.getLayoutParams();
paramsUp.topMargin = 0;
myBar.setLayoutParams(paramsUp);
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams paramsMove = (RelativeLayout.LayoutParams) myBar.getLayoutParams();
paramsMove.topMargin = Y - yDelta;
myBar.setLayoutParams(paramsMove);
break;
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) myBar.getLayoutParams();
yDelta = Y - lParams.leftMargin;
break;
}
//myBar.invalidate();
return true;
}
}
When I moving container, everything happens very slowly. This is because the container changes its size and all objects are loaded again.
And my second question:
How to disbale scroll when I move ListView item? For example: move on left or move on right.
Thx
I'm building an Android app where I have to edit a picture which has been picked from the phone gallery and cropped.
So in my layout, I have an ImageView where I am placing the cropped image
xml file
<ImageView
android:id="#+id/ivEditPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight=".90"
android:src="#drawable/app_icon_big" />
Placing the cropped image in ImaveView
bitmapPic = (Bitmap) getIntent().getParcelableExtra(
"CroppedBitmapImage");
picView = (ImageView) findViewById(R.id.ivEditPhoto);
picView.setImageBitmap(bitmapPic);
The image is getting placed correctly. But the problem is when I try to edit it.
I have an edit button and on click of that I do the following which includes registering of On Touch Listener.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Bitmap alteredPastedBitmap = Bitmap.createBitmap(bitmapPic.getWidth(),
bitmapPic.getHeight(), bitmapPic.getConfig());
pasteCanvas = new Canvas(alteredPastedBitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);
matrix = new Matrix();
pasteCanvas.drawBitmap(bitmapPic, matrix, paint);
picView.setOnTouchListener(this);
Then the following
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(this, v.getId(), Toast.LENGTH_SHORT).show();
if (v.getId() == R.id.ivEditPhoto) {
Toast.makeText(this, "onTouch", Toast.LENGTH_SHORT).show();
int action = event.getAction();
x = event.getX();
y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
sX = event.getX();
sY = event.getY();
break;
case MotionEvent.ACTION_UP:
if (skewedBitmap == null) {
int resID = 0;
if (imageId == 0)
resID = R.drawable.green_arrow;
else
resID = imageId;
bitmapToPaste = BitmapFactory.decodeResource(
getResources(), resID);
} else {
bitmapToPaste = skewedBitmap;
skewedBitmap = null;
}
pasteCanvas.drawBitmap(bitmapToPaste, sX- (bitmapToPaste.getWidth() / 2),sY- (bitmapToPaste.getHeight() / 2), null);
picView.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
}
return true;
}
The issue is:
the bitmapPic.getWidth() and bitmapPic.getHeight() is 160*160 and the onTouch event.getX(); and event.getY(); is beyond the co-ordinates (For example: 150.33 & 500.89) although the image seems to be occupying the whole screen and the touch is on the image. So onTouch doesn't place the bitmap and returns a false.
Could any of you please guide me on this?
I had the same issue in one of my applications. I need to see more of your code but this could be related to the onInterceptTouch() method you need to implement.
If that is not the case, then maybe you can look into this code for help on how to move, modify and rotate bitmaps:
http://adblogcat.com/custom-view-to-draw-rotate-erase-and-convert-images-to-black-and-white/
I wrote this tutorial btw and this is my website and I think is easier for you to follow that tutorial than just paste it here.