I have created a drag and drop application that allows the user to drag 4 images from the top and put them in order at the bottom. I have 4 image views on top and 4 imageviews on the bottom. Bringing them down works perfectly, however when I attempt to move them side to side I run into problems.
I have 2 temporary imageviews where I set them equal to a imageview. If i move an image from image view 6 to image view 5, it is supposed to swap the images, but it just changes image view 6 to what image view 5 was and image view 5 stays the same.
Here is the code snippet to where I am attempting to change the images
droppedSwap is equal to the image the user chose to move
dropTargetSwap is equal to where the user wants the image to go
if (dropTargetSwap.equals(ivHero5) && droppedSwap.equals(ivHero6))
{
//set temp imageview that is equal to ivHero5
ImageView tempDropTarget = ivHero5;
//set temp imageview that is equal to ivHero6
ImageView tempDropped = ivHero6;
//supposed to set ivHero6 to ivHero5 image
droppedSwap.setBackground(tempDropTarget.getBackground());// working
//supposed to set ivHero5 to ivHero6 image
dropTargetSwap.setBackground(tempDropped.getBackground());// not working
}
You're problem is that by doing:
ImageView tempDropTarget = ivHero5;
ImageView tempDropped = ivHero6;
You're not actually creating temporary copies but rather references to the original objects, so when you do droppedSwap.setBackground() you're actually setting the ivHero6 background and when you do tempDropped.getBackground() you're getting the ivHero6 background that was changed earlier.
You should create a copy of the backgrounds, which are drawables using:
Drawable copy1 = ivHero5.getBackground().getConstantState().newDrawable();
Drawable copy2 = ivHero6.getBackground().getConstantState().newDrawable();
Related
TL;DR: It looks fine when setting a drawable to ImageView (see attachment - top). Replacing it with a Bitmap (setImageBitmap) and setting again to the original (setImageBitmap(null) & setBackgroundResource(R...)) leads to the drawable being stretched (see atachment - bottom).
The ImageView element has a resource image when starting the app and it looks fine and not stretched. If the user puts his sign by using another activity, the ImageView is used to show a small thumbnail of his sign and the ImageView gets overwritten by using setImageBitmap().
The issue occurs, when user saves the draft and all fields get emptied. Emptying includes setImageBitmap(null)
and setBackGroundResource(R.drawable.ACTUAL_IMAGE_OF_IMG_VIEW).
After resetting the ImageView (deleting Bitmap and setting actual background), the Background-Image gets stretched (see attached picture):
Changing the orientation of the device (landscape and portrait) the image is normal again.
Also there are grid lines on the background of the image, which absolutely doesn't stem from the ressource image.
Would love to hear the way You would solve this.
Edit - Code:
// Inserting Bitmap
private void insertSignature(){
ivSign.setImageBitmap(signature);
}
// setting signature -> null and adding icon again as background
private void ResetInputFields(){
ivSign.setImageBitmap(null);
ivSign.setBackgroundResource(R.drawable.sign_pen);
// TRIED ALREADY:
// ivSign.setBackground(getResources().getDrawable(R.drawable.sign_pen));
}
add to your imageView's XML code
android:scaleType="fitCenter"
or
android:scaleType="centerInside"
and it will keep its original ratio
In Short
I have an ImageView with size set to wrap_content.
The image has a drawable already set, and everything looks great.
Now, I want to change the drawable, while forcing the ImageView to keep it's size.
Explanation
Well, the easy answer is of course to set the width & height to a fixed size.
But in my case, I am copying an ImageView + layout from a Google Map to add my custom map button. It works well, because I have an icon with same size.
Now I want to change the icon for a moment (loading icon btw), but because the Drawable source is larger, it changes the ImageView size accordingly.
ScaleType doesn't work as the width and height are not fixed.
What I've Tried
1. Setting the new Drawable bounds using getBounds on the old one.
2. setRight() on the ImageView with the right bound of the existing map button (the original one which doesn't change).
(left is already fixed, it's inside a RelativeLayout).
Some (working) Code of how I created the new button
ImageView newBtn = new ImageView(context);
newBtn.setAdjustViewBounds(false);
newBtn.setBackgroundResource(R.drawable.map_btn_background);
newBtn.setScaleType(ScaleType.CENTER_INSIDE);
newBtn.setImageResource(imageResId);
newBtn.setClickable(true);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationBtn.getLayoutParams();
rlp = copyRelativeLayoutParams(rlp);
//add to Google's map button layout
btnContainer.addView(newBtn, rlp);
Some (not working) Code of how I change the drawable.
This is the point were I want the ImageView to keep it's size.
perfectlyMadeNewMapButtonImageView.setImageDrawable(mLoadingDrawable);
//mLoadingDrawable.setBounds(
// perfectlyMadeNewMapButtonImageView.getDrawable().getBounds());
// not working
I'm using this code to dynamically pop images on the slider using Image Flipper.
flipper= (ViewFlipper) findViewById(R.id.flipper1);
for(int i=0;i<6;i++)
{
// This will create dynamic image view and add them to ViewFlipper
Button image = new Button(getApplicationContext());
image.setBackgroundResource(R.drawable.stock_bg);
image.setText("Sps"+i);
// image.getLayoutParams().height=40;
// image.getLayoutParams().width=40;
flipper.addView(image);
}
I have set sliding animation to it. The issue is that it fits one image on the whole screen width and then animates to other. While I want the image size to remain same and it should not fit it to the entire screen. Also note that if I change the flipper width from wrap content to custom width, it will not slide the entire screen. Putting it simple, how can I force the flipper to show more then 1 image on the screen at a time.
i have 2 imageview. first one for containing the image and the other one as the frame.
i already set image position to have the same position as the frame after loading the image.
i also already check the value and its the same.
but somehow the image position is not the same as the frame when i try changing picture with 2 different size (width x height).
ivProfilePicture.setX(ivFrame.getX());
ivProfilePicture.setY(ivFrame.getY());
by the way, i use custom imageview for the image container, i only custom the function onMeasure. there's no image repositioning.
Well after days of looking I come to the source.
I have main screen that is drawn and created in Eclipse Graphical layout.
It consists of buttons across the top of the screen. Below the buttons is image.png area.
On startup of app it displays correctly. Buttons at top and graphic image below the buttons.
On pushing each button I want the image to be changed. I can supply either a drawable or bitmap image (figured that out).
I need help. What I don't understand is how I get an external image to display with the XML generate screen buttons.
I have currently many test trials but none give me what I am looking for.
I want to place either a drawable or bitmap image into R.id.imgVwMain and have it display with the buttons. I know I've got extra code in what is provided.
R.id.imgVwMain as a default has an image in it at start up (ie splash screen) but I want to replace it dynamically on button push.
Code snippets:
ImageView img=new ImageView(this);
View image;
Drawable drawable= getImg(0); // GETS NEW IMAGE as drawable
img.setImageDrawable(drawable);
//setContentView(img); // when uncommented displays just the image no buttons, not what needed
//R.id.imgVwMain // the target ID to place the new image into
image = findViewById(R.id.imgVwMain);
ImageView imageView=(ImageView)findViewById(R.id.imgVwMain);
imageView.setImageBitmap(drawableToBitmap(drawable));
image.setImageResource(imageView);
setContentView(R.layout.main); //of course this just displays default of the XML
Set onClickListeners onto your buttons like so:
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView ivImage = (ImageView) findViewById(R.id.imgVwMain);
ivImage.setImageDrawable(getImg(0));
}
});
Now, whenever the button is clicked it set's the image of your imageview to whatever you'd like.
You can probably use addView to add an imageView dynamically and likewise call removeView to get rid of an imageView. At some point you'll need to set the layout parameters as well. Hopefully this will get you started:
myParentViewOrLayout.removeView(oldImage);
ImageView newImage = (ImageView) findViewById(R.id.imgToAdd);
//Set your layoutParams
myParentViewOrLayout.addView(newImage);