How To Transition object on RelativeLayout Using Android Studio? - java

I want Transition my object called ball inside ImageView on ReltiveLayout when I touch ReltiveLayout then ball move to center of point has been touch
public class MainActivity extends AppCompatActivity {
//ImageView.W(70dp) ImageView.H(70dp) to get center touch
ImageView ball;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball = (ImageView)findViewById(R.id.ball);
layout = (RelativeLayout) findViewById(R.id.layout);
layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//ImageView.W(70dp) ImageView.H(70dp)
//touch X,Y-70 to move ball to center touch
ball.setX(motionEvent.getX()-70);
ball.setY(motionEvent.getY()-70);
return false;
}
});
}
}

Related

Pinch zoom only zooms into center

I‘m trying to make an image in my app pinch-zoomable. It should be able to zoom into different parts of the image. As the code is right now, wherever I‘m doing the zooming gesture it only zooms into the center, so I have no chance to move the image or zoom into other areas.
What is necessary decenter the zoom and move the zoomed image?
public class MainActivity extends AppCompatActivity {
private ScaleGestureDetector mScaleGestureDetector;
private float mScaleFactor = 1.0f;
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView)findViewById(R.id.imageView);
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
mImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mScaleGestureDetector.onTouchEvent(event);
return true;
}
});
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector){
mScaleFactor *= scaleGestureDetector.getScaleFactor();
mScaleFactor = Math.max(1.0f,
Math.min(mScaleFactor, 1.7f));
mImageView.setScaleX(mScaleFactor);
mImageView.setScaleY(mScaleFactor);
return true;
}
}
}
For your purposes, the best will be added this library:
https://github.com/chrisbanes/PhotoView
Of course, or add this library as local module and customize it for your purposes.

How can I move an image based on a user's touch?

I am trying to get an image to move, either to the Left Or Right, when the user touches either the Left Or Right of their device's screen. I have the following code....I have run the Emulator, in Android Studio, and when I click on the Right or Left sides of the Emulator's Screen....nothing happens. What is wrong with this code? All answers are welcome! I have entered the following code in the Activity that has the image I want to move:
public class GameScreen1 extends AppCompatActivity implements View.OnTouchListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen1);
ImageView circle1 = (ImageView) findViewById(R.id.circle1);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.circle1:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//WHAT CODE SHOULD I PUT INSTEAD OF THE FLOAT X AND X++
int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
float Xtouch = event.getRawX();
int sign = Xtouch > 0.5*ScreenWidth ? 1 : -1;
float XToMove = 50;
int durationMs = 50;
v.animate().translationXBy(sign*XToMove).setDuration(durationMs);
}
break;
}
return false;
}
}
Add ID to your root layout in the activity and add TouchListener on it.
Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/cl_root"
android:layout_height="match_parent"
tools:context=".MainActivity">
</android.support.constraint.ConstraintLayout>
And This is the code of your activity:
public class MainActivity extends AppCompatActivity {
ConstraintLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.cl_root);
layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int x = (int)event.getX();
if ( x >= ( screenWidth/2) ) {
//Right touch
}else {
//Left touch
}
return false;
}
});
}
}

Webview to load while slashscreen is on

Quite new to android studio but i'm facing a problem where my WebView is taking too long to load, so i've created a splashscreen for 4 seconds and plan to cover the loading using this splash screen. But I dont really know how to let it overlap so the WebView can load behind the splashscreen.
the java for my splash screen
public class MainActivity extends ActionBarActivity {
private static int SPLASH_TIME_OUT = 4000;
BottomBar mBottomBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run(){
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
}
}
then i also have a bottombar that navigates to the first framgment with the webview and the java are as below
public class HomeActivity extends ActionBarActivity {
BottomBar mBottomBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
mBottomBar = BottomBar.attach(this,savedInstanceState);
mBottomBar.setItemsFromMenu(R.menu.menu_main,new OnMenuTabClickListener() {
#Override
public void onMenuTabSelected(#IdRes int i) {
if(i==R.id.Bottombaritemone) {
HomeFragment f = new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
}
else if(i==R.id.Bottombaritemtwo)
{
SearchFragment f =new SearchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame,f).commit();
}
else if(i==R.id.Bottombaritemthree)
{
AccountFragment f =new AccountFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame,f).commit();
}
}
#Override
public void onMenuTabReSelected(#IdRes int i) {
}
});
mBottomBar.mapColorForTab(0,"#F2F2F2");
mBottomBar.mapColorForTab(1,"#F2F2F2");
mBottomBar.mapColorForTab(2,"#F2F2F2");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
and finally the java for my webview in the first HomeFragment
public class HomeFragment extends Fragment {
WebView my2WebView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home,container,false);
getLoaderManager();
my2WebView = (WebView) view.findViewById(R.id.webView1);
WebSettings webSettings = my2WebView.getSettings();
my2WebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setJavaScriptEnabled(true);
webSettings.getLoadsImagesAutomatically();
my2WebView.loadUrl("http://www.fidellaglobal.com/feed");
my2WebView.setWebViewClient(new WebViewClient());
return view;
}
so what can i do to making the webview in the first fragment to load when the splashscreen is on ? thank you :)
Initially set your WebView visibility invisible inside your layout file. Add you WebView in same screen which have delay Handler.
new Handler().postDelayed(new Runnable() {
#Override
public void run(){
// Make your webview visible
mWebView.setVisibility(View.VISIBLE);
}
},SPLASH_TIME_OUT);

I want to get TextView reference in class but i get exceptions

Here is the code for my mainActivity.java file
I am trying to access the reference of textview in class MyGestureListener but i am unable to do this because every time my application unfortunately stopped on scroll gesture.
here is my code
This is my main class
public class MainActivity extends AppCompatActivity {
TextView myText;
Button myButton;
private GestureDetectorCompat mDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView)findViewById(R.id.myText);
myButton = (Button)findViewById(R.id.myButton);
mDetector = new GestureDetectorCompat(this, new MyGestureListener());
myButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
myText.setText("I am clicked from button");
}
});
}
public TextView getTextView()
{
TextView txtView = (TextView)findViewById(R.id.myText);
return txtView;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
This is my user defined class
class extends GestureDetector.SimpleOnGestureListener {
private TextView myText;
MainActivity m;
public MyGestureListener(){
m = new MainActivity();
}
//MainActivity m = new MainActivity();
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
myText.setText("Scroll ho v ja hun");
return true;
}
}
You must not create any Activity objects yourself, as you do in the MyGestureListener constructor. They are created by Android.
Also in MyGestureListener the TextView is never initialised.
You should instead pass a reference to the TextView when you initialise a MyGestureListener object.
For example you can do something like this:
public class MainActivity extends AppCompatActivity {
TextView myText;
Button myButton;
private GestureDetectorCompat mDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView)findViewById(R.id.myText);
myButton = (Button)findViewById(R.id.myButton);
mDetector = new GestureDetectorCompat(this, new MyGestureListener(myText));
myButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
myText.setText("I am clicked from button");
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private TextView myText;
public MyGestureListener(TextView textView) {
myText = textView;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
myText.setText("Scroll ho v ja hun");
return true;
}
}
You are never setting the myText MyGestureListener class. Try this:
myText = (TextView)findViewById(R.id.myText);
mDetector = new GestureDetectorCompat(this, new MyGestureListener());
mDetector.myText = myText;
For the code above to work myText will have to be public.
So change this:
private TextView myText;
to this:
public TextView myText;

How to use touch and double click on the image..?

I have one imageview, if i touch the imageview it to do some action else if i double click that imageview need to do some action.
How it is possible.?
First create a GestureDetector and a listener. Then bind it to your class that extends GestureDetector.SimpleOnGestureListener.
private GestureDetector detector;
private ImageView mImageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detector = new GestureDetector(this, new MyGesturesListener());
// TODO find your image view
mImageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
}
Then you can implement the methods of different gestures:
class MyGesturesListener extends GestureDetector.SimpleOnGestureListener{
#Override
public boolean onSingleTapUp(MotionEvent ev) {
// TODO handle single tap
return true;
}
#Override
public boolean onDoubleTap(MotionEvent ev) {
// TODO handle double tap
return true;
}
}

Categories