I am trying to make game for which there are buttons with alphabets and if user swipes over them then it should detect the views as well as make a line across them. I searched many tutorials, examples and questions but unable to get the idea. I have attached image to help understand the question.
Here are the codes:
MainActivity.java
package com.example.detecttouch;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView btn1, btn2;
TextView txtresult;
Rect outRect = new Rect();
int[] location = new int[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
txtresult = (TextView) findViewById(R.id.txtresult);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {}
});
btn1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_UP){
if(isViewInBounds(btn2, x, y))
btn2.dispatchTouchEvent(event);
else if(isViewInBounds(btn1, x, y)){
Log.d("Panauti", "onTouch ViewA");
//Here goes code to execute on onTouch ViewA
}
}
// Further touch is not handled
return false;
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {}
});
btn2.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_UP){
if(isViewInBounds(btn1, x, y))
btn1.dispatchTouchEvent(event);
else if(isViewInBounds(btn2, x, y)){
Log.d("Panauti", "onTouch ViewB");
//Here goes code to execute on onTouch ViewB
}
}
// Further touch is not handled
return false;
}
});
}
private boolean isViewInBounds(View view, int x, int y){
view.getDrawingRect(outRect);
view.getLocationOnScreen(location);
outRect.offset(location[0], location[1]);
return outRect.contains(x, y);
}
}
and activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/txtresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="102dp"
android:textColor="#android:color/holo_red_dark"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="A" />
<TextView
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"
android:text="B" />
</RelativeLayout>
Please Help. Thanks in advance.
I suggest making a layout that contains the four TextViews and then setting up an onTouchListener to the layout containing them. That way, it can draw a line whenever the layout is touched rather than a specific button and can migrate between views:
private int startX, startY, endX, endY; // fields to determine the coordinates of the Rect
layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startX = event.getRawX();
startY = event.getRawY();
} else {
endX = event.getRawX();
endY = event.getRawY();
// set line bounds
}
}
});
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello this is my first question so please forgive me if its in bad format, I have my main activity with three buttons,Button 1 and 2 lead to numberpickers that allow user to pick from numbers to set a background image. My problem is once they pick the color (rgb) i need it sent back to the main activity but then i want the button to immediately change to the background color they chose then they can go to button two and do the same thing, at the end of the process both button1 and 2 should be the colors they have chosen. Which i will then mix in button 3(which i havent at all started on) My main problem is i pick one then it set but then i pick the other one and it onCreates() and sets the other back to a default , So i somehow need to change the colors immedietly after the activity returns to the main activity my code is a bit ugly but ive just been trying to get it working then i was gonna go back and make it more efficient so my apologize
//MAIN CONTENT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="50dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mixButton"
android:id="#+id/screen1MixerButton"
android:layout_below="#+id/screen1Button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="103dp" />
<Button
android:text="#string/Color1Text"
android:layout_width="162dp"
android:layout_height="188dp"
android:id="#+id/screen1Button1"
android:src="#ffffff"
android:background="#ffffff"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<Button
android:text="#string/Color2Text"
android:layout_width="162dp"
android:layout_height="188dp"
android:id="#+id/screen1Button2"
android:background="#ffffff"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/screen1Button1" />
</RelativeLayout>
//COLORRPICKER1 ACTIVITY
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.ColorPicker1"
tools:showIn="#layout/activity_color_picker1">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/numberPicker1"></NumberPicker>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/numberPicker2"
android:orientation="horizontal"></NumberPicker>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/numberPicker2"
android:layout_alignParentEnd="true"
android:orientation="horizontal"
android:id="#+id/numberPicker3"></NumberPicker>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="71dp"
android:id="#+id/surfaceView1"
android:layout_gravity="center_vertical"
android:background="#000000"
android:layout_below="#+id/numberPicker1"
android:layout_alignParentStart="true"
android:layout_marginTop="99dp"></SurfaceView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Color"
android:id="#+id/PickColor1"
android:layout_below="#+id/surfaceView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
//COLORPICKER2 activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.ColorPicker2"
tools:showIn="#layout/activity_color_picker2">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/numberPicker1"></NumberPicker>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/numberPicker2"
android:orientation="horizontal"></NumberPicker>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/numberPicker2"
android:layout_alignParentEnd="true"
android:orientation="horizontal"
android:id="#+id/numberPicker3"></NumberPicker>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="71dp"
android:id="#+id/surfaceView2"
android:layout_gravity="center_vertical"
android:background="#000000"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"></SurfaceView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Color"
android:id="#+id/PickColor2"
android:layout_below="#+id/surfaceView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp" />
</RelativeLayout>
//MAIN ACTIVITY
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;'
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MYTAG";
public static Button colorButton1;
private Button colorButton2;
private Button mixerButton;
int red1, green1, blue1,red2, green2, blue2 =255;
public static final int REQUEST_CODE= 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "in onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//START SETTING UP BUTTONS
colorButton1 = (Button) findViewById(R.id.screen1Button1);
colorButton2 = (Button) findViewById(R.id.screen1Button2);
mixerButton = (Button) findViewById(R.id.screen1MixerButton);
//ONCLICKLISTENER FOR MULTIPLE BUTTONS
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.screen1Button1:
Log.i(TAG, "screen1Button1 TAPPED");
Toast.makeText(MainActivity.this, "screen1Button1 TAPPED", Toast.LENGTH_LONG).show();
Intent i1 = new Intent(MainActivity.this,ColorPicker1.class);
startActivityForResult(i1,REQUEST_CODE);
break;
case R.id.screen1Button2:
Log.i(TAG, "screen1Button2 TAPPED");
Toast.makeText(MainActivity.this, "screen1Button2 TAPPED", Toast.LENGTH_LONG).show();
Intent i2 = new Intent(MainActivity.this,ColorPicker2.class);
startActivity(i2);
Log.i(TAG, "BACKFROM1");
break;
case R.id.screen1MixerButton:
Log.i(TAG, "screen1MixerButton TAPPED");
Toast.makeText(MainActivity.this, "screen1MixerButton TAPPED", Toast.LENGTH_LONG).show();
Intent i3 = new Intent(MainActivity.this,MixedColorScreen.class);
startActivity(i3);
Log.i(TAG, "test value recieved" + red1);
Log.i(TAG, "test value recieved" + green1);
Log.i(TAG, "test value recieved" + blue1);
Log.i(TAG, "test value recieved" + red2);
Log.i(TAG, "test value recieved" + green2);
Log.i(TAG, "test value recieved" + blue2);
red1 = getIntent().getIntExtra("ColorPicker1_red",0);
green1 = getIntent().getIntExtra("ColorPicker1_green",0);
blue1 = getIntent().getIntExtra("ColorPicker1_blue",0);
red2 = getIntent().getIntExtra("ColorPicker2_red",0);
green2 = getIntent().getIntExtra("ColorPicker2_green",0);
blue2 = getIntent().getIntExtra("ColorPicker2_blue",0);
colorButton1.setBackgroundColor(Color.rgb(red1, green1, blue1));
colorButton2.setBackgroundColor(Color.rgb(red2, green2, blue2));
break;
}
}
};
colorButton1.setOnClickListener(onClickListener);
colorButton2.setOnClickListener(onClickListener);
mixerButton.setOnClickListener(onClickListener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "in onCreateOptionsMenu");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "in onOptionsItemsSelected");
// 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//colorpicker1 activity
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;
public class ColorPicker1 extends AppCompatActivity {
private static final String TAG = "MYTAG";
NumberPicker np, np2, np3;
SurfaceView img;
int red, green, blue ;
private Button pickColorButton1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_picker1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pickColorButton1 = (Button) findViewById(R.id.PickColor1);
pickColorButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {;
Intent colorPick1 = new Intent(ColorPicker1.this,MainActivity.class);
colorPick1.putExtra("ColorPicker1_red",red);
colorPick1.putExtra("ColorPicker1_green", green);
colorPick1.putExtra("ColorPicker1_blue", blue);
//TRY
//MainActivity.colorButton1 = (Button) findViewById(R.id.screen1Button1);
//colorPick1.setBackgroundColor(Color.rgb(red, green, blue));
//END TRY
Log.i(TAG, "PUTING IN PUTEXTRAS VALUES RED BLUE GREEN =" + red +green + blue);
startActivity(colorPick1);
}
});
//STARTING LOGS ----------------------------------------------------------------------------
Log.i(TAG,"IN COLORPICKER1");
Toast.makeText(ColorPicker1.this, "IN COLORPICKER1", Toast.LENGTH_LONG).show();
//SETUP NUMBERPICKERS-----------------------------------------------------------------------
np = (NumberPicker) findViewById(R.id.numberPicker1);
np.setMinValue(0);
np.setMaxValue(255);
np.setWrapSelectorWheel(true);
np2 = (NumberPicker) findViewById(R.id.numberPicker2);
np2.setMinValue(0);
np2.setMaxValue(255);
np2.setWrapSelectorWheel(true);
np3 = (NumberPicker) findViewById(R.id.numberPicker3);
np3.setMinValue(0);
np3.setMaxValue(255);
np3.setWrapSelectorWheel(true);
//VALUE CHANGED LISTENERS-------------------------------------------------------------------
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
red = np.getValue();
img = (SurfaceView) findViewById(R.id.surfaceView1);
img.setBackgroundColor(Color.rgb(red, green, blue));
}
});
np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
green = np2.getValue();
img = (SurfaceView) findViewById(R.id.surfaceView1);
img.setBackgroundColor(Color.rgb(red, green, blue));
}
});
np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
blue = newVal;
img = (SurfaceView) findViewById(R.id.surfaceView1);
img.setBackgroundColor(Color.rgb(red, green, blue));
}
});
//CLOSE-------------------------------------------------------------------------------------
}
}
//colorpicker2 activity
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;
public class ColorPicker2 extends AppCompatActivity {
private static final String TAG = "MYTAG";
NumberPicker np, np2, np3;
SurfaceView img;
int red, green, blue ;//might not need
private Button pickColorButton2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_picker2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pickColorButton2 = (Button) findViewById(R.id.PickColor2);
pickColorButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ColorPicker2.this, "COlorPick2", Toast.LENGTH_LONG).show();
Intent colorPick2 = new Intent(ColorPicker2.this,MainActivity.class);
colorPick2.putExtra("ColorPicker2_red",red);
colorPick2.putExtra("ColorPicker2_green", green);
colorPick2.putExtra("ColorPicker2_blue", blue);
Log.i(TAG, "PUTING IN PUTEXTRAS VALUES RED BLUE GREEN =" + red + green + blue);
startActivity(colorPick2);
}
});
//STARTING log -----------------------------------------------------------------------------
Log.i(TAG, "IN COLORPICKER2");
Toast.makeText(ColorPicker2.this, "IN COLOPICKER2", Toast.LENGTH_LONG).show();
//SETTING UP NUMBER PICKERS ----------------------------------------------------------------
np = (NumberPicker) findViewById(R.id.numberPicker1);
np.setMinValue(0);
np.setMaxValue(255);
np.setWrapSelectorWheel(true);
np2 = (NumberPicker) findViewById(R.id.numberPicker2);
np2.setMinValue(0);
np2.setMaxValue(255);
np2.setWrapSelectorWheel(true);
np3 = (NumberPicker) findViewById(R.id.numberPicker3);
np3.setMinValue(0);
np3.setMaxValue(255);
np3.setWrapSelectorWheel(true);
//SETTING UP LISTENERS ---------------------------------------------------------------------
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
red = np.getValue();
img = (SurfaceView) findViewById(R.id.surfaceView2);
img.setBackgroundColor(Color.rgb(red, green, blue));
}
});
np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
green = np2.getValue();
img = (SurfaceView) findViewById(R.id.surfaceView2);
img.setBackgroundColor(Color.rgb(red, green, blue));
}
});
np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
blue = newVal;
img = (SurfaceView) findViewById(R.id.surfaceView2);
img.setBackgroundColor(Color.rgb(red, green, blue));
}
});
//end --------------------------------------------------------------------------------------
}
}
For this you have to call startActivityForResult every time when you open ColorPicker1/2/3
Like this
Intent i1 = new Intent(MainActivity.this,ColorPicker1.class);
startActivityForResult(i1,REQUEST_CODE_COlLOR1);
//REQUEST_CODE must be greater than`0` so keep it `101` for example
and for ColorPicker2 keep REQUEST_CODE_COlLOR2 as 102 and for ColorPicker3 REQUEST_CODE_COlLOR2 as 103
and in your ColorActivity1/2/3 Call intent like this with respective request codes
Intent i = new Intent();
i.putExtra("color1", yourColor);
setResult(REQUEST_CODE_COlLOR1, i);
finish();
for in Second color activity
Intent i = new Intent();
i.putExtra("color2", yourColor);
setResult(REQUEST_CODE_COlLOR2, i);
finish();
In your mixed
Intent i = new Intent();
i.putExtra("color3", yourColor);
setResult(REQUEST_CODE_COlLOR3, i);
finish();
and then write OnActivityResult Override method in MainActivity to get the result from ColorPicker1/2/3
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_CODE_COlLOR1) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
//Set your color on your first button
yourColor = data.getExtras().get("color1");
}
}
if (requestCode == REQUEST_CODE_COlLOR2) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
//Set your color on your second button
yourColor2 = data.getExtras().get("color2");
}
}
if (requestCode == REQUEST_CODE_COlLOR3) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
//Set your color on your mixed color button
yourColor3 = data.getExtras().get("color3");
}
}
}
I have two TouchImageView. What I want to achieve is that, When I swipe first TouchImageView from left to right, second TouchImageView get swiped right to left automatically. I am using TouchImageView.java class which I have founded at GitHub. Below is my code.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2"
tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >
<com.PinchZoom.pinchzoomexampletwo.TouchImageView
android:id="#+id/img_to_be_zoomed"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/fb" >
</com.PinchZoom.pinchzoomexampletwo.TouchImageView>
<com.PinchZoom.pinchzoomexampletwo.TouchImageView
android:id="#+id/img_to_be_zoomed_mirror"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/fb" >
</com.PinchZoom.pinchzoomexampletwo.TouchImageView>
</LinearLayout>
MainActivity.java
package com.PinchZoom.pinchzoomexampletwo;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.PinchZoom.pinchzoomexampletwo.TouchImageView.OnTouchImageViewListener;
public class MainActivity extends ActionBarActivity {
TouchImageView img_to_be_zoomed;
TouchImageView img_to_be_zoomed_mirror;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_to_be_zoomed = (TouchImageView) findViewById(R.id.img_to_be_zoomed);
img_to_be_zoomed
.setOnTouchImageViewListener(new OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomed_mirror.setZoom(img_to_be_zoomed);
Log.e("X Scroll Point: ",
"" + img_to_be_zoomed.getScrollPosition());
}
});
img_to_be_zoomed_mirror = (TouchImageView) findViewById(R.id.img_to_be_zoomed_mirror);
img_to_be_zoomed_mirror
.setOnTouchImageViewListener(new OnTouchImageViewListener() {
#Override
public void onMove() {
// TODO Auto-generated method stub
img_to_be_zoomed.setZoom(img_to_be_zoomed_mirror);
PointF s = new PointF();
PointF q = new PointF();
s = img_to_be_zoomed.getScrollPosition();
q = img_to_be_zoomed_mirror.getScrollPosition();
img_to_be_zoomed.setScrollPosition(s.x
- (q.x - img_to_be_zoomed.getX()), s.y);
Log.e("img_1", s.x + "/" + s.y);
Log.e("img_2", "" + q.x + "/" + q.y);
}
});
}
}
TouchImageView.java
TouchImageView.java Find Here
Edit:
I have solved problem using below code in MainActivity.java.
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed.getScrollPosition();
img_to_be_zoomed_mirror.setScrollPosition(1 - pointF_img1.x, pointF_img1.y);
Try this to get the center point of an imageview.
int width=Bitmap.getWidth();
int height=Bitmap.getHeight();
int centerX=width/2;
int centerY=height/2;
I have solved problem using below code in MainActivity.java.
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed.getScrollPosition();
img_to_be_zoomed_mirror.setScrollPosition(1 - pointF_img1.x, pointF_img1.y);
I am trying to capture the latitude of an image that has been captured through the camera and stored into the ImageBox variable. Using the ExifInterface class, I am trying to get the latitude of the image and display that information into the LatitudeBox variable but my syntax is incorrect so please let me know what I am doing wrong! full Code below
MainActivity.java
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
//variables
Button CameraButton;
ImageView ImageBox;
EditText LatitudeBox, LongitudeBox;
String IMG = ImageBox.toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CameraButton = (Button) findViewById(R.id.CameraButton);
ImageBox = (ImageView) findViewById(R.id.imageView1);
LatitudeBox = (EditText) findViewById(R.id.LatitudeBox);
LongitudeBox = (EditText) findViewById(R.id.LongitudeBox);
//Camera Button Onclick
CameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
try {
ExifInterface exif = new ExifInterface(IMG);
LongitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF)); // get longitude
LatitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF)); //get latitude
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// try catch
}// end onclick
});// end camera button onclick
}// close oncreate
//display image inside ImageBox variable using bitmap
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap TheImage = (Bitmap) data.getExtras().get("data");
ImageBox.setImageBitmap(TheImage);
}
}
#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;
}
}// close main
App now crashes on launch and not sure what's causing it. Here is an image of the logcat errors
http://s7.postimg.org/4p67papyj/logcat1.png
activity_main.XML
<ImageView
android:id="#+id/imageView1"
android:layout_width="250dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/CameraButton"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="#string/Camera_Button" />
<TextView
android:id="#+id/LatitudeBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/CameraButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="#string/LatitudeBox"
android:textColor="#ff0000"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/LongitudeBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/LatitudeBox"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="#string/LongitudeBox"
android:textAppearance="?android:attr/textAppearanceLarge" />
There is no .get() method for ExifInterface.
LatitudeBox = exif.get(ExifInterface.TAG_GPS_LONGITUDE_REF);
Did you perhaps mean:
LatitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF));
EDIT:
I found it.
Look at the last variable here:
Button CameraButton;
ImageView ImageBox;
EditText LatitudeBox, LongitudeBox;
String IMG = ImageBox.toString();
You haven't assigned a value to your ImageView ImageBox. So when you ImageBox.toString(), you receive a NullPointerException.
Change initialization of IMG to something like:
String IMG = "filename.txt";
I want to display the images front and back ward. Like Windows Picture and Fax Viewer This is my code:
package com.my.imagechange;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class ImageChangeDemo extends Activity
{
/** Called when the activity is first created. */
// private Gallery gallery;
private ImageView imgView;
int i=0;
int j=6;
ImageButton btrt,btlt;
Button bt1,bt2;
private Integer[] Imgid ={R.drawable.androidlogo,R.drawable.androids, R.drawable.cool, R.drawable.cupcake2009, R.drawable.donut2009, R.drawable.eclair2009};
int imglength=Imgid.length;
//System.out.println(imglength);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imagechange);
System.out.println(imglength);
btlt=(ImageButton)findViewById(R.id.bt1);
btrt=(ImageButton)findViewById(R.id.bt2);
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
btlt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int choice1=j--;
switch(choice1)
{
case 5:imgView.setImageResource(Imgid[5]);
break;
case 4:imgView.setImageResource(Imgid[4]);
break;
case 3:imgView.setImageResource(Imgid[3]);
break;
case 2:imgView.setImageResource(Imgid[2]);
break;
case 1:imgView.setImageResource(Imgid[1]);
break;
}
}
});
btrt.setOnClickListener(new View.OnClickListener()
{ #SuppressWarnings("unused")
#Override
public void onClick(View v)
{
int choice2=i++;
switch(choice2)
{
case 1:imgView.setImageResource(Imgid[1]);
break;
case 2:imgView.setImageResource(Imgid[2]);
break;
case 3:imgView.setImageResource(Imgid[3]);
break;
case 4:imgView.setImageResource(Imgid[4]);
break;
case 5:imgView.setImageResource(Imgid[5]);
break;
}
}
});
}}
It works forward fine, but when I click backward it doesn't work. Where did I make my mistake? Can anybody tell me?
Thanks in advance.
Please change like this
int choice1=--j;
Edit:
Here the problem is after execution of this code int choice1=j--; choice1 contain value 6, which is not in the switch case
Hi now the code works finely. My code is
package com.my.imagechange;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class ImageChange extends Activity
{
private Gallery gallery;
private ImageView imgView;
private ImageButton btlt,btrt;
private Integer[] Imgid= {
R.drawable.androidlogo, R.drawable.androids, R.drawable.cool, R.drawable.cupcake2009, R.drawable.donut2009, R.drawable.eclair2009};
int imglength=Imgid.length;
int img_position;
int img_minus;
int img_plus;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
btlt=(ImageButton)findViewById(R.id.bt1);
btrt=(ImageButton)findViewById(R.id.bt2);
System.out.println("----->"+imglength +"----->");
btlt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
btrt.setEnabled(true);
System.out.println("left button has been clicked");
System.out.println("position: "+img_position);
img_minus=--img_position;
System.out.println("minus:"+img_minus);
imgView.setImageResource(Imgid[img_minus]);
//imgView.setBackgroundDrawable(getResources().getDrawable(id));
if(img_minus==0)
{
btlt.setEnabled(false);
}
}
});
btrt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
btlt.setEnabled(true);
System.out.println("right button has been clicked");
System.out.println("position: "+img_position);
img_plus=++img_position;
System.out.println("plus: "+img_plus);
imgView.setImageResource(Imgid[img_plus]);
if(img_plus==5)
{
btrt.setEnabled(false);
}
}
});
} }
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#ffffff">
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/androidlogo"/>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:layout_gravity="fill_horizontal"
>
<ImageButton android:id="#+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/butleft"
android:layout_alignParentLeft="true">
</ImageButton>
<ImageButton android:id="#+id/bt2"
android:src="#drawable/butright"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true">
</ImageButton>
</RelativeLayout>
</LinearLayout>