How to move a view in circle with onTouch in android - java

I am facing a problem in an project. I have a activity in which i had create a Round shape layout with Framelayout. My problem is that i don;t know how to implement items in layout in round shape. and move the items with ontouch event and when the items come on the specific postion a toast display or alert dialog show. I am attach the image for demo.

What you are looking for is a View that implements the onTouch event with a Drag feature.
1) In order to give your view the round shape, you need to create a file that contains those specifications in the drawable folder.
Create your drawable/your_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
</shape>
2) In your View that you want to make round, set the background to that same drawable like
<YourView
android:id="#+id/your_id"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/your_circle"/>
And then, apply the following procedure to the Activity
public class DraggableActivity extends Activity {
float dX;
float dY;
int lastAction;
View.OnTouchListener touchListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag_view_layout);
// 1 - Create the touch listener
touchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN) {
Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
}
break;
default:
return false;
}
return true;
}
};
// 2 - Add a reference to your view that already is stated on the layout
final View dragView = findViewById(R.id.your_id);
// 3 - Attac the the TouchListener to your view
dragView.setOnTouchListener(this);
}
}
Let me know if it works.
Regards,

Related

How to continuously move object in Android Studio

How to continuously move an ImageView in Android Studio when a Button is held down, but stop when it isnt clicked anymore? With other words: how to detect if a button is "unclicked§ or direktly detect if it is held down. Thanks for the help
To detect if a button is pressed/released(down/up), TouchListener is used.
imageView = findViewById(R.id.image);
button = findViewById(R.id.button);
root_layout = findViewById(R.id.root_layout); //parent layout
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
#Override
public void run() {
float x = imageView.getX();
if(x > root_layout.getWidth())
x = 0;
else
x += 6; //Increase value of '6' to move the imageView faster
imageView.setX(x);
handler.postDelayed(this,0); //increase delay '0' if facing lag.
// This is the rate at which the x value of our imageView is being updated
}
};
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
handler.post(runnable); //Start moving imageView
break;
case MotionEvent.ACTION_UP:
handler.removeCallbacks(runnable); //Stop moving imageView
break;
}
return true;
}
});
Try this solution
Step 1:- need to define anim folder in your res folder.
now place rotate.xml file in anim folder.
rotate.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="600"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="#android:anim/cycle_interpolator"/>
</set>
Step 2:- Declare animation programmatically
// Animation
Animation animFadein;
in onCreate()
// load the animation
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
Step 3:- Detect user touch like this to start and stop animation
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//start animation here
imageView.startAnimation(animFadein);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// stop animation here
imageView.stopAnimation(animFadein);
}
return true;
}
});

Simply Swipe any Normal Android View without third party library

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 to make a button unclickable but Touchable?

I have a button, when I click it it makes a scale animation, and also I can drag it with onTouch event.
I want to make it unclickable after first touch and this code does. Here OnClick
btnScale.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
v.startAnimation(animScale);
btnScale.setClickable(false);
}});
Also this code help me to drag it. Here OnTouch
btnScale.setOnTouchListener(new Button.OnTouchListener(){
public boolean onTouch(View v, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
oldXvalue = me.getX();
oldYvalue = me.getY();
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
//RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
params.leftMargin = (int) (me.getRawX() - (v.getWidth() / 2));
params.topMargin = (int) (me.getRawY() - (v.getHeight()));
//LayoutParams params = new LayoutParams(v.getHeight(), (int) (me.getRawX() - (v.getWidth() / 2)), (int) (me.getRawY() - (v.getHeight())));
v.setLayoutParams(params);
}
return false;
}});
However I still want to drag it after I click it. However when I make it unclickable it cannot be dragable since it is unTouchable too
EDIT:
When I use if else state on onClick as boolean variable, I still able to click the button and a ugly square appears in my circular button.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#61d5ee">
<item>
<shape android:shape="oval">
<solid android:color="#61d5ee"/>
<corners android:radius="5dp" />
<stroke android:width="4px" android:color="#cdf3fb" />
</shape>
</item>
</ripple>
What to do?
Create a boolean to say if it is enabled or not, change manually the state in your onClick and change the button's design too. In your Onclick use something like:
if(enabled){
do something
}else{
//do nothing
}
You don't need the else, it's just an example.
Then... he is touchable enabled and disabled.

Android OnDraw and onTouchEvent not working together

I am new to android development as well as the Java programming language and would definitely appreciate some help with this issue.
Currently I have some code which creates a canvas and draws a bitmap picture onto it, this appears to work. I also have another bit of code which detects where the users finger is on the devices screen and this also works. when I try to combine the two the bitmap will display but as soon as I click the screen the app crashes.
I have a feeling that this issue is with the canvas/bitmap side of this as i have also noticed that any widgets that have added to layout vanish when the canvas/bitmap feature is running.
This is the code for the canvas/bitmap:
public class myView extends View{
Bitmap image;
DisplayMetrics metrics;
public myView(Context context) {
super(context);
metrics = context.getResources().getDisplayMetrics();
// TODO Auto-generated constructor stub
image = BitmapFactory.decodeResource(getResources(), R.drawable.bhead);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Matrix matrix = new Matrix();
float angle = 90;
float imageCenterX = 50;
float imageCenterY = 50;
matrix.setRotate(angle, imageCenterX, imageCenterY);
Bitmap temp;
temp = BitmapFactory.decodeResource(getResources(), R.drawable.bhead);
image = Bitmap.createScaledBitmap(temp, 100, 100,true);
matrix.postTranslate(((width/2)-50), ((height/2)-100));
canvas.drawBitmap(image, matrix, null);
}
}
and this is the code for the finger location on the screen:
public class MainActivity extends ActionBarActivity {
myView mview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mview = new myView(this);
setContentView(mview);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
EditText txt = (EditText)findViewById(R.id.editText1);
txt.setText("X = "+ x +" Y = " + y);
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Your view hierarchy does not have an EditText with the id editText1. This is because you have set the content view to be a single instance of your class MyView (class names should start with uppercase) with this code in onCreate():
mview = new myView(this);
setContentView(mview);
As a result, you get a NullPointerException on the line txt.setText("X = "+ x +" Y = " + y); because the previous line cannot find the EditText.
You need to include both views in the layout. You can use MyView in XML by writing its fully qualified name. For example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.your.package.name.MyView
android:id="#+id/my_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
</LinearLayout>

HOW TO disable getView() method in ListView Adapter?

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

Categories

Resources