I need to apply some text on the part of the image where a touch event is occurred. Its just like the Image Tagging on facebook. Please provide some clue how to go about it.
What I've done so far is that I am loading image from gallery and showing it on image viewer.
public class LoadImg extends Activity implements OnTouchListener{
private static int RESULT_LOAD_IMAGE = 1;
float x,y;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
x=0;y=0;
Button buttonLoadImage = (Button) findViewById(R.id.button1);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
Implementing the OnTouchListener, I m locating the x and y coordinates .
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x=event.getX();
y=event.getY();
return false;
}
But imageviewer doesnt has any method to add text on the coordinates.
I dont want to preset a text/toast. I want to get a edittext/toast to get generated when a user touches the image.
Get the x, y positions using event.getX(), event.getY().
Then, at those positions add a textview to the layout. Like:
TextView tv=new TextView(this);
add layout params and left margin and top margin with those x, y values
LinearLayout.LayoutParams trparams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
trparams.leftmargin=x;
trparams.topmargin=y;
tv.setLayoutParams(trparams);
then add this textview to the layout.
If you want to do it, you can use a longClickListener and show a toast message with your tip string in it..
Toast viewToast = Toast.makeText(this, "Your tool tip", Toast.LENGTH_SHORT);
yourView.setOnLongClickListener(new OnLongClickListener() {
#Override
public void onLongClick(View v) {
viewToast.show();
}
});
I am developing an animation app like Mine Sweeper. Upon tapping on the buttons, the buttons will be pressed and the user may tap on the other button. On the third tap on the chosen button, there will be an incoming image (which uses AlphaAnimation/ScaleAnimation) from that button.
Buttons
private int[] right_lung = { R.id.lungs_106, R.id.lungs_113,
R.id.lungs_114, R.id.lungs_115, R.id.lungs_116, R.id.lungs_121,
R.id.lungs_122, R.id.lungs_123, R.id.lungs_124, R.id.lungs_125,
R.id.lungs_129, R.id.lungs_130, R.id.lungs_131, R.id.lungs_132,
R.id.lungs_133, R.id.lungs_134, R.id.lungs_137, R.id.lungs_138,
R.id.lungs_139, R.id.lungs_140, R.id.lungs_141, R.id.lungs_142,
R.id.lungs_145, R.id.lungs_146, R.id.lungs_147, R.id.lungs_148,
R.id.lungs_149, R.id.lungs_150, R.id.lungs_151, R.id.lungs_152,
R.id.lungs_153, R.id.lungs_154, R.id.lungs_155, R.id.lungs_156,
R.id.lungs_157, R.id.lungs_158, R.id.lungs_159, R.id.lungs_160,
R.id.lungs_161, R.id.lungs_162, R.id.lungs_163, R.id.lungs_164,
R.id.lungs_165, R.id.lungs_166, R.id.lungs_167, R.id.lungs_168,
R.id.lungs_169, R.id.lungs_170, R.id.lungs_171, R.id.lungs_172,
R.id.lungs_173, R.id.lungs_174, R.id.lungs_175, R.id.lungs_176,
R.id.lungs_177, R.id.lungs_178, R.id.lungs_179, R.id.lungs_180,
R.id.lungs_181, R.id.lungs_182, R.id.lungs_183, R.id.lungs_184,
R.id.lungs_185, R.id.lungs_186, R.id.lungs_187, R.id.lungs_188,
R.id.lungs_189, R.id.lungs_190, R.id.lungs_191, R.id.lungs_192,
R.id.lungs_194, R.id.lungs_195, R.id.lungs_196, R.id.lungs_197,
R.id.lungs_198, R.id.lungs_199, R.id.lungs_200, R.id.lungs_205,
R.id.lungs_206, R.id.lungs_207 };
private Button[] button_right_lung = new Button[right_lung.length];
MainActivity
for (int i = 0; i < button_right_lung.length; i++) {
final int b = i;
button_right_lung[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
show_first_page();
int[] startPosition = new int[2];
button_right_lung[b].getLocationOnScreen(startPosition);
int[] endPosition = new int[2];
button_right_lung[b].getLocationOnScreen(endPosition);
Animation anim = new ScaleAnimation(startPosition[1], 2000, endPosition[1], 2000);
anim.setDuration(3000);
img_Message1.setVisibility(View.VISIBLE);
img_Message1.startAnimation(anim);
}
});
}
Now, my question is, how can I get the coordinates of the selected button and will animate the image after the button is pressed?
Now you know how many butouns you need to put. You also have id for all of them. You have placed same in an array.
Now I do believe that you can find array length.
Just put all buttouns in loop.
for ex:
for(int i = 0; i < right_lung.length; i++){
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button btn = (Button)v;
int btn_value = btn.getid;
// Some thing like that..
}
});
}
you can use OnTouchListener on your button
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int X=(int) event.getX();
int Y=(int) event.getY();
}
}
i want to touch button and drag into other button position you can get clear idea by this image.
Guiding Image
Please help me
I Also Found This very helpful.
Touch and drag image in android
but i want to drag to other button position not some were else on screen.i don't have any idea how i would detect button is entered in other button dimensions.
So far i am doing is just animation but i don't want just straight forward moving button by click.
i want user to move by himself button to other button position.
Code
public class AnimationActivity extends Activity implements OnClickListener {
public Button btn_a1, btn_a2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainsec);
btn_a1 = (Button) findViewById(R.id.btn_a1);
btn_a2 = (Button) findViewById(R.id.btn_a2);
btn_a1.setOnClickListener(this);
btn_a2.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_a1: {
int direction = -1;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
.getLayoutParams();
final float xDelta = (displaymetrics.widthPixels / 2)
- absParams.leftMargin - (btn_a1.getWidth() / 2);
final Animation animation = new TranslateAnimation(0, xDelta
* direction, 0, 0);
AnimationListener animationOutListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
btn_a2.setBackgroundDrawable(R.id.blank);// Unselect
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btn_a2.setBackgroundDrawable(R.id.red);// Select
}
};
animation.setAnimationListener(animationOutListener);
animation.setDuration(1000);
btn_a2.startAnimation(animation);
break;
}
case R.id.btn_a2: {
int direction = 1;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
.getLayoutParams();
final float xDelta = (displaymetrics.widthPixels / 2)
- absParams.leftMargin - (btn_a1.getWidth() / 2);
final Animation animation = new TranslateAnimation(0, xDelta
* direction, 0, 0);
AnimationListener animationOutListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
btn_a1.setBackgroundDrawable(R.id.blank);// Unselect
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btn_a1.setBackgroundDrawable(R.id.red);// Select
}
};
animation.setDuration(1000);
btn_a1.startAnimation(animation);
break;
}
}
}
How about this:
You would have to evaluate the buttons parentViews onTouchEvents, and if you hit it, you can start the onClick method associated with the button, or when you lift your finger, what ever suits your app.
But while finger down/move you can just change the buttons coordinates and trigger a redraw ont he parent view.
You might have to create a new View that supports moving Buttons. I dont know if you can do that inside of a layout.
public class UnitConverterActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
LinearLayout mLinearLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.mainmenu);
//i.setAdjustViewBounds(false);
i.setScaleType(ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(i);
setContentView(mLinearLayout);
//setContentView(R.layout.main);
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
I have used the above method to load an image for the main menu I am trying to create. The image has four areas and each will be used to call a particular function of the app. Now I am trying to implement touch interface on those areas. I know how to define the range of pixels for that purpose but I am at loss on how to implement OnTouchListner on the image. Please help me in this regard.
If your image was split into four rectangular quarters (say)
then in onCreate have:
i.setOnTouchListener(this);
and for your listener, something like this (illustrates the principle only):
#Override
public boolean onTouch(View v, MotionEvent mev) {
int width = v.getWidth();
int height = v.getHeight();
float x = mev.getX();
float y = mev.getY();
String msg;
if (x < width / 2) {
if (y < height / 2)
msg = "Top left quarter";
else
msg = "Bottom left quarter";
} else {
if (y < height / 2)
msg = "Top right quarter";
else
msg = "Bottom right quarter";
}
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
return false;
}
Just put this code in onCreate().
i.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//your code
}
}
I want to zoom text that is displayed at center of screen as per user choice. How can I achieve this ?
Using pinch multitouch cannot be tested on emulator and I want something that I can test on Android emulator.
Can I use zoom in and out controls to control only text view for my layout ?
Or Can I use webview to contain a text as webview has default zoom in out buttons ?
Can I use zoom in and out controls to
control only text view for my layout ?
There is no built-in support for that. You can probably achieve this effect yourself by drawing the text using the 2D graphics APIs (Canvas) and touch events. Or, intercept touch events on a TextView and change the font size of the text.
This is my answer for that
public class Songs extends SherlockActivity implements OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
context = this;
sett = new SettRenderingEngine(context);
Intent i = getIntent();
song = i.getStringExtra("song");
singer = i.getStringExtra("singer");
lyrics = i.getStringExtra("lyrics");
txt_song_title = (TextViewPlus) findViewById(R.id.song_title);
txt_singer = (TextViewPlus) findViewById(R.id.singer);
txt_song_lyrics = (TextViewPlus) findViewById(R.id.song_lyrics);
txt_song_title.setText(sett.getSettString(song));
txt_singer.setText(sett.getSettString(singer));
txt_song_lyrics.setText(sett.getSettString(lyrics));
txt_song_lyrics.setOnTouchListener(this);
scaleGestureDetector = new ScaleGestureDetector(this,
new simpleOnScaleGestureListener());
}
public class simpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
float size = txt_song_lyrics.getTextSize();
float factor = detector.getScaleFactor();
float product = size * factor;
txt_song_lyrics.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
size = txt_song_lyrics.getTextSize();
return true;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
return true;
}
}
For Zoom In Zoom Out
public class ZoomControls extends AppCompatActivity
{
Button zoomin, zoomout;
TextView text;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoomcontrols);
zoomin = (Button) findViewById(R.id.zoomin);
zoomout = (Button) findViewById(R.id.zoomout);
text = (TextView) findViewById(R.id.text);
zoomin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float x = text.getScaleX();
float y = text.getScaleY();
text.setScaleX((float) (x + 1));
text.setScaleY((float) (y + 1));
}
});
zoomout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float x1 = text.getScaleX();
float y1 = text.getScaleY();
text.setScaleX((float) (x1 - 1));
text.setScaleY((float) (y1 - 1));
}
});
}
}