Android SDK - convert bitmap to resource for layout params issue - android

For some reason, when I create a bitmap from an image in asset, it does not display fullscreen. When I take an image from drawable, resId, the image shows fullscreen. Why is this?
//the image is not fullscreen
ImageView iv = new ImageView(getBaseContext());
iv.setImageBitmap(bitmapFileFromAssetorSD);
//this makes the image fullscreen
//iv.setBackgroundResource(resId);
iv.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT
)
);

Try setting the appropriate ImageView.ScaleType
ImageView.setScaleType(ImageView.ScaleType.CENTER_CROP)

Related

how to removing programmatically composed pictures

I create imageview programmatically. but after I finish game this imageview cannot close how can i close, remove this imageview.
if I use this code image.setImageResource(0); just last image closing but I want to close all image
final ImageView image = new ImageView(getContext());
image.setImageResource(id);
image.setLayoutParams(params);
layout.addView(image);
Great!!! This is my solution..
` final ImageView [] image = new ImageView[5];
`
image[userartis]=new ImageView(getActivity());
image[userartis].setImageResource(id); image[userartis].setLayoutParams(params);
layout.addView(image[userartis]);
for (int i=0;i<=userartis;i++){
image[i].setVisibility(View.GONE);
}

Covering android ImageView

While I was working on my project I found that the image couldn't cover the whole space of ImageView completely! this is the first time that i have got a problem like this.
Here is my code :
int ImgId = getResources().getIdentifier("test1", "drawable", getPackageName());
Photo=new LinearLayout(this);
lp = new LinearLayout.LayoutParams(Width/2,Width/2);
Photo.setLayoutParams(lp);
Photo.setOrientation(LinearLayout.VERTICAL);
Photo.setBackgroundColor(getResources().getColor(R.color.mainGreen));
Img = new ImageView(this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
Img.setLayoutParams(lp);
Img.setBackgroundColor(getResources().getColor(R.color.Black));
Img.setImageResource(ImgId);
Photo.addView(Img);
I have used this piece of code many times before. The image covered the ImageView's space but now its not working well!
What is the problem? How can I cover ImageView completely?
Img.setBackground(ImgId);
//Img.setImageResource(ImgId);
or
Img.setBackground(ImgId);
Img.setScaleType(ImageView.ScaleType.FIT_XY);
Img.setImageResource(ImgId);
Just add ScaleType to the ImageView to specify the behavior
Img.setScaleType(ImageView.ScaleType.CENTER_CROP);

Set ImageView height of ListView Header

I'm currently trying to set the height of an ImageView, placed as ListView header, but i'm always getting an NullPointerExpection in imageHeaderView.getLayoutParams. Here's my code:
ImageView imageHeaderView = new ImageView(this);
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.deckblatt));
imageHeaderView.setScaleType(ScaleType.CENTER_CROP);
imageHeaderView.getLayoutParams().height = 50;
myList.addHeaderView(imageHeaderView);
Is it not possible to get/set the layoutParams, when used in a ListView header?
What else can i do to set the height of my ImageView?
Best regards
try this--->
ImageView imageHeaderView = new ImageView(this);
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.deckblatt));
imageHeaderView.setScaleType(ScaleType.CENTER_CROP);
imageHeaderView.setLayoutParams(new AbsListView.LayoutParams(100, 100));
myList.addHeaderView(imageHeaderView);

Adding ImageView leads to the black screen

I have the problem: when you try to add ImageView the LinearLayout, some images are not loaded and the program stops with a black screen. Picture JPG or PNG there is no difference. Tried to change the size of 100x100px to 1080x1920. If I replace the picture on another then everything is fine, but I need this picture. I put the exception to this code, but in the logcat nothing, i.e. the exception does not occur.
Please help me. Thank you.
for (int i = 0, lenI = anims.length; i < lenI; i++ ) {
try {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//add image
ImageView imageView = new ImageView(this);
linearLayout.addView(imageView);
int resId = getResources().getIdentifier("ru.romston.testprog:drawable/"+pics[i], null, null);
imageView.setImageResource(resId);
imageView.setMaxHeight(100);
imageView.setMaxWidth(100);
imageView.setPadding(5,5,5,5);
imageView.setAdjustViewBounds(true);
//label
TextView textView = new TextView(this);
linearLayout.addView(textView);
textView.setText(anims[i]);
layout.addView(linearLayout);
} catch (Exception e) {
Log.e(TAG, "read data: error!" + e.getMessage(), e);
}
}
I think the image is of higher resolution/size that Android rendering system fails to scale it into height and width of 100. Try with same image after changing the image size/resolution.
Please correct if wrong.
ImageViews have a resolution limit based on OpenGL. If the bitmap or image passes this limit resolution, it shows a black screen and throws no error.
You can retrieve this limit by querying OpenGL:
int[] maxSize = new int[1];
GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
See this answer for more details

Android - setBackgroundResource on Button

I have some buttons which i set the background with setBackgroundResource(R.drawable.cal_box_center); and the problem I'm having is that my background is a gradient which has this banding effect (annoying) and I've read that in order to remove this you'll need to set the Bitmap.Config.ARGB_8888. I looked into the API and the way to do this is to use decodeStream etc, but how can i use setBackgroundResource and still set the Config to ARGB_8888?
Thanks in advance.
You can use this code snippet:
// create button
Button btn = new Button(getApplicationContext());
//decode the resource(You can also use decodeStream and other decode method of
//BitmapFactory)
Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.cal_box_center);
//create another copy of your bitmap and specify Config
Bitmap newBtm = btm.copy(Bitmap.Config.ARGB_8888, true);
//use your newBtm to create a BitmapDrawable
BitmapDrawable btmDrwble = new BitmapDrawable(newBtm);
// finally set the drawable as your button's background
btn.setBackgroundDrawable(btmDrwble);

Categories

Resources