I'd appreciate it if someone could help me with my problem.
I am trying to change the image in an ImageView when someone clicks on it. I've put my images in an array and I am using a while loop to cycle once through all of them.
My problem is that while the first image (image8, not in the array) shows in the view all the other (after creating the OnClickListener) do not. Actually nothing happens and I am not sure where the mistake is. Thanks in advance.
This is the problematic code:
final int array[]=new int[5];
array[0]= R.drawable.image6;
array[1]= R.drawable.image4;
array[2]= R.drawable.image9;
array[3]= R.drawable.image4;
array[4]= R.drawable.image5;
ImageView touchView = (ImageView)findViewById(R.id.imageview);
touchView.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View touchView, MotionEvent ev) {
//get coordinates of touch event
int x = (int)ev.getRawX();
int y = (int)ev.getRawY();
---Code missing---
((ImageView)touchView).setImageResource(R.drawable.image8);
touchView.setOnClickListener(new View.OnClickListener() {
int counter = 0;
#Override
//Image change on every click
public void onClick(View touchView) {
while(counter<5){
((ImageView) touchView).setImageResource(array[counter]);
counter++;
});
You can't change images in a sequence like that and expect anything to show on the screen. You should use the click to start a separate thread that will do the image animation. See the description of the android.view.animation package. It sounds like the AnimationDrawable class will give you exactly what you want.
There is a special view for your case. ImageSwitcher is what you need. There is an example from android developers on how to use it. It should be trivial to adapt the example to your needs.
Related
Please help me understand why getDrawingCache() is returning null when called on my image button view (appcompatimagebutton) from within my ontouchlistner attached to my image button view.
Mainly, in trying to solve this, I have been referring to this question:
Android View.getDrawingCache returns null, only null
which has not solved my problem. I am sure I am overlooking something or not fully understanding something. Please help me figure out what it is.
Notes:
1) it seems to make no difference weather i use ImageButton or AppCompatImageButton (but i am using appcompat because in the actual project I need features it offers that ImageButton does not).
2) I do not see why i should need to call the view's measure and layout methods since the layout is already painted to the screen by the time a user would fire ontouch() but thought i'd include it anyway as many answers i found about seemed to indicate this was required.. it doesn't work if i take them out either.
3) it does not appear to matter if I place the setEnableDrawingCache and buildDrawingCache calls in the onTouch or in the onCreate with the AppCompatImageButton view initialization. In either case, my ontouchlistener call to the image button view's getDrawingCache returns null.
here is the code sample,
Thanks!
public class MainActivity extends AppCompatActivity {
AppCompatImageButton mImageButton;
Bitmap mBitmapDrawingCache;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create image button
mImageButton = new AppCompatImageButton(this);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(270, 270);
mImageButton.setLayoutParams(layoutParams);
//getDrawingCache() in ontouchlistener returns null regardless if this is set
int mseWidth = View.MeasureSpec.makeMeasureSpec(270, View.MeasureSpec.EXACTLY);
int mseHeight = View.MeasureSpec.makeMeasureSpec(270, View.MeasureSpec.EXACTLY);
mImageButton.measure(mseWidth, mseHeight);
mImageButton.layout(0, 0, 270, 270);
/* this does not work either
int mseWidth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int mseHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mImageButton.measure(mseWidth, mseHeight);
mImageButton.layout(0, 0, mImageButton.getMeasuredWidth(), mImageButton.getMeasuredHeight());
*/
//getDrawingCache() in ontouchlistner returns null regardless if either, both or none of these are set
//mImageButton.buildDrawingCache();
mImageButton.setDrawingCacheEnabled(true);
//attach image button view to content view
((FrameLayout) findViewById(android.R.id.content)).addView(mImageButton);
//set touch listener
mImageButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//why is this line returning null instead of a Bitmap ?
mBitmapDrawingCache = Bitmap.createBitmap(v.getDrawingCache());
Log.d("TAG_A", mBitmapDrawingCache.toString());
return false;
}
});
}
}
In order to get bitmap out from your View, the order in which you generate cache is something like this:
mImageButton.setDrawingCacheEnabled(true);
mImageButton.buildDrawingCache();
mBitmapDrawingCache = Bitmap.createBitmap(mImageButton.getDrawingCache());
You need to call this all together inside OnTouchListener or more preferably OnClickListener (if you want to generate bitmap only when clicked).
However, I'm not sure why you are calling all the MeasureSpec stuff... If you are loading the image through setContentView(R.layout.activity_main); then don't be bothered with all manual measuring.
I'm extremely new to Android programming. Right now I'm building a very simple app so i can get the hang of things.
Basically i have:
- Background
- ImageView
- ImageButton.
Every time I click the ImageButton, it cycles through a list of images I have stored in an array. However I'm ending up skipping a lot of frames.
private static ImageButton button;
private static ImageView current;
private int index;
int[] images = {R.drawable.image1, R.drawable.image2};
public void buttonClick() {
current = (ImageView)findViewById(R.id.imageView);
button = (ImageButton)findViewById(R.id.imageButton);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
current.setImageResource(images[index]);
if(index == (images.length - 1)) {
index = 0;
}
else {
index++;
}
}
}
);
}
Any idea what is causing it to skip frames, and what I can do to fix it? Is it because my image files are too big? They are about 788kb each and 1920x1120.
Thank you.
You must be doing it off the ui thread.
Images/Bitmaps should be efficiently loaded.
Refer example on official page: https://developer.android.com/training/displaying-bitmaps/index.html
Yes. The size is the image is probably causing the skipping of frames. Try using smaller images to verify that your images are the problem.
I am new to android, In my app I just need to add a screen like pattern lock, what exactly I want to do is, no need to create/ set a pattern.Just design a screen with 9 dots, and when user draw some pattern and I need to get those pattern value.For design I used PatternView but how can I get the user drawer pattern? I already google on this, but I found there they setting the pattern lock, I don't want that.I just want is when I draw something on PatternView it just return the value like,suppose like if I draw a pattern like 2365 dots it will return an integer value like 2365.
I already tried haibison.github.io/android-lockpattern but my app is different than this.I just want to draw a pattern and get result, no need to create and confirm screens. No need to save the result in shared preferences. I just want compare the drawn pattern value with my predefined value.
Following is idea on how to go for this:
Start with a table view for your layout and create a 3 x 3 grid of image views (dots).
override onTouchListener on your activity to detect touches on images. refer this answer for details.
private ImageView imageView;
private Rect imageRect;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (imageRect == null) {
imageRect = new Rect();
imageView.getGlobalVisibleRect(imageRect);
}
int x = (int) event.getX();
int y = (int) event.getY();
if (imageRect.contains(x, y)) {
Log.i(TAG, "touch passing over imageView");
}
return true;
}
example is for 1 image view you have to build it for 9.
as and when touch is detected append the index of imageview to a string.
once an image is touched stop detecting touches on it.
perform desired operation when MotionEvent.ACTION_UP is detected
Hi I have loaded dynamically images in different position using single image view instance.
my problem is if I touch particular image second image also affected 1st I wish to find which image is touched. i want to print which image is touched. I am suffer in this task please help me.
my problem is i have only one imageview to show all images in appropriate location. I received xml file that xml contain everything like image size, image position ...etc I am loading dynamically images because i receive xml file dynamically. if i touch one image i wish to identify which image is clicked in the design that's all
This is my screen shot:
Firtly implements onTouchListener then override the method,
ImageView one = ....
ImageView two = ....
one.SetTag("ITEM ONE");
two.SetTag("ITEM TWO");
one.setOnTouchListener(this);
two.setOnToucListener(this);
#Override
public boolean onTouch(View v, MotionEvent event) {
if(v == one)
{
Log.e("Touched Itemd: ",(String) v.getTag().toString());
}
if(v == two)
{
Log.e("Touched Itemd: ",(String) v.getTag().toString());
}
}
Hello guys I want to use a ImageView as a slideshow, that touch you on the right of the screen display the next image and if the touch is on the left show the previous image,
I already tried to implement this function but I get no response from the ImageView, looking logcat I realized that it doesn't dispatch of TouchEvent.
public class Prova4Activity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
ImageView image;
Bitmap trash = loadImageFromUrl("http://www.televideo.rai.it/televideo/pub/tt4web/ Nazionale/16_9_page-100.6.png");
ImageView imgpag;
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.EDGE_RIGHT){
trash.recycle();
trash = null;
trash = loadImageFromUrl("http://www.televideo.rai.it/televideo/pub/tt4web/Nazionale/16_9_page-100.6.png");
image.setImageBitmap(trash);
}else if(event.getAction() == MotionEvent.EDGE_LEFT){
trash.recycle();
trash = null;
trash = loadImageFromUrl("http://www.televideo.rai.it/televideo/pub/tt4web/ Nazionale/16_9_page-100.5.png");
imgpag.setImageBitmap(trash);
}
imgpag.setScaleType(ScaleType.FIT_XY);
return true;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgpag = (ImageView)findViewById (R.id.imgpag);
imgpag.setImageBitmap(trash);
imgpag.setOnTouchListener(this);
}
}
I would not use a gallery because the images are small and do not know in advance the number of images because i load them directly from Inernet based on the actions of the user
This might be not the best way to achieve this but you can try following:
Set two buttons on Screen - One on Right corner and One on the left corner.
(If you are showing ImageView full screen then better to use FrameLayout -
bt make sure that Buttons are on the front)
Set Button properties like
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="40dp" android:background="#null"/>
This will make sure that buttons are invisible
Then just add your logic of prev and next on OnClickListeners of these buttons
Hope this will help you