Having trouble with moving a drawable in Android - java

I'm having trouble with some code I've set up that allows me to move an imageView's drawable to another imageView simply by clicking on the two imageViews (one with source image and then destination imageView) in succession.
The goal is to simulate a chessboard.
My starting display is the following:
And after I click the leftmost pawn then click the square directly in front of it, the result is the following. You'll see that the target imageView (first column 3rd row) is losing its horizontal boundary definition and expanding across the entire tableRow:
Now, I'm also resetting tags along with the drawables in each square but I'd imagine that the issue stems from the way in which I'm getting and setting the drawables.
My code is as follows:
int selectedId = getResources().getIdentifier(selectedSquare, "id", getPackageName());
ImageView sourceSquare = (ImageView) findViewById(selectedId);
Drawable sourceDrawable = sourceSquare.getDrawable();
ImageView targetSquare = (ImageView) findViewById(R.id.s20);
targetSquare.setImageDrawable(sourceDrawable);
My guess is that the sourceDrawable variable I'm creating somehow grabs too much or too little information and when I move that drawable into the new imageView targetSquare this destroys my formatting.

You can give the new ImageView the source LayoutParams of the origin:
targetSquare.setLayoutParams(sourceSquare.getLayoutParams);

Related

What am I missing when trying to apply a grayscale filter to ImageView?

According to multiple sources this code should work on an ImageView. An ImageButton is a child class of an ImageView, so this should not be a factor. Just in case, I have tried changing the view to an ImageView. Either way, there is no effect and the image remains in full color.
ImageButton imageButton = findViewById(R.id.imageButton);
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageButton.setColorFilter(filter);
I found the problem: my view did not have an src, only a background, which is to say, it had:
android:background="#drawable/jw_true"
instead of:
android:src="#drawable/jw_true"
These are interchangeable for many purposes, but not for this. When my attempts were not working, the filter was being applied to the non-existent src, which is why nothing happened.

ImageView onClick animation doesn't work after change in resource

I'm new to android, and I am currently working on a simple connect three game, I have nine ImageViews which contain transparent images, when the ImageView is clicked, the resource is changed to x or o.
I've tried adding more animation, and setting resource to null rather than a transparent image, but it didn't work, only restarting the activity seems to fix it.
public void oneClick (View view)
{
//image view onClick
ImageView one = (ImageView) findViewById(R.id.one);
one.setImageResource(R.drawable.x); //setting x or y
one.animate().rotation(180).setDuration(500); //animation
}
Here's how I reset the images,
one.setImageResource(R.drawable.transp)
After this, if the onClick activity is called again, the image is set.
However the animation doesn't seem to work.
What am I doing wrong?
My image was rotated 180, so it wasn't rotating again, the simple solution was to reset the orentation of the image then run the rotate animation again, which seems to have fixed it!
clicked.setImageResource(R.drawable.x);
clicked.animate().rotation(180).setDuration(500);
after this
clicked.animate().rotation(0);
now if I use the rotation animation again, it seems to work!
This is a simple fix. Simply add .start() to your animation. It should look like this afterward:
one.animate().rotation(180).setDuration(500).start();

How to create a button with round edges programatically in Java?

Is it possible to create a button with round edges without using a shape XML in my drawable folder?
My app has a lot of buttons that will change colors based on what the user assigns and touches. This will mean I will have to create a lot of shapes in my xml for it's specific color (have about 20 colors).
Or is there a way I can easily change the background color of my shape's button? I have about 45 buttons on one page of in my app.
You can use imageView.setColorFilter(Color.RED) function to change color of xml drawable
here are some example
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );

How to define a clickable area in an image used in ImageView

Is there a way to define a clickable area in an image? Something equivalent or similar to map-area tags in HTML? Or is there a standard way of doing this sort of thing?
A possible method
1) get the width, and height of the image view using (in pixels)
2) get the coordinates of the image view (which is the top left corner of the image view)
3) create a touch listener, and record the coordinates of the users touch
4) if the user's touch coordinates fall on the image do whatever you want to
If you only want to do certain things based on where the user touches you will have to calculate the coordinates of those regions using the coordinates of the image view, and the image views width and height.
I think there might be no standard way for this, but you can achieve it by placing a transparent VIEW over your imageview. Set click listener to that view and make imageview clickable false.
When i have to do something similar, i use an include view.
As for the layout the include view will be accessing, i would use FrameLayout as root.
Its children would be the ImageView that i want to depict, along with a LinearLayout infront of it containing Buttons at the positions i want the user to choose from.
Then in code, i set listeners to these buttons.
Lastly, i would set the buttons so that there background is transparent and text is not available.
For example in a project i am working on, i have an ImageView with an Image of Ocean as background.
In front of the view i have a LinearLayout with smaller ImageViews with images of islands.
I place listener on the islands to see when they are clicked to open a new activity.

Can an View play its animation out of its parent?

I have some ImageViews in a TableLayout's TableRow. When I start an animation of one ImageView using the following code:
ImageView image = (ImageView)findViewById(id);
TranslateAnimation a = new TranslateAnimation(0, 0, 0, 80);
a.setDuration(500);
image.startAnimation(a);
the image is expected to move downwards. But unfortunately it's in a TableRow, so I can see the image moves "in the backstage of the row". Any ideas? I can't use other layouts because of a lot of existing codes.
The answer is no. Animations change properties of an view, but can't take it out of its parent. At last I gave up and use another layout.

Categories

Resources