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
Related
Hello i am showing images in android app using gallery. I am having problem in displaying images. Some images are displayed some are not.
My adapter code is as follows:
ImageView iv;
LinearLayout layoutnew = new LinearLayout(getApplicationContext());
layoutnew.setOrientation(LinearLayout.VERTICAL);
if (convertView == null)
{
iv = new ImageView(ctx);
iv.setBackgroundDrawable(m_docs_ids[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
int temp =(int) (height/1.7f);
int temp_y = (int) ((3*temp)/2.0f);
iv.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ));
}
else
{
iv = (ImageView) convertView;
}
TextView tv = new TextView(ctx);
// tv.setText(m_docs.get(arg0).DocPath);
tv.setTextColor(0xFFFFFFFF);
tv.setPadding(0, 15, 0, 0);
tv.setTag(arg0);
tv.setTextSize(18);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ));
tv.setGravity(Gravity.CENTER);
layoutnew.addView(iv);
layoutnew.addView(tv);
return layoutnew;
m_docs_id is a list of drawable type. i have debug it and found that it is getting correct entry. gallery is showing black for images which are not shown.
I found put that pictures placed in DCIM folders are not shown. all other images are shown
you initialize the imageViewlike this iv = new ImageView(ctx);
you should initilize the imageView like this iv = (ImageView) findViewById(R.id.mImageView);
Finally i got my problem solved. Issue was not in DCIM folder, issue was in size. The pictures taken from cell phone camera is very high and gallery was not getting those images. I set size in drawables of photo with mobile screen size. Now its running f9
I have looked at various questions on this but, couldn't get them work.
I have a button, with a background, image on top and text at the bottom. The button is dynamic and hence NO XML is available.
Code for the dynamic button:
final Button image = new Button(this);
image.setTag(i);
image.setText(buttonsList[i].toUpperCase());
image.setGravity(Gravity.CENTER);
image.setBackgroundColor(getResources().getColor(R.color.dark_grey));
if (i == Const.MainIndex.FESTIVAL_OUTLOOK_INDEX) {
try {
XmlResourceParser parser = getResources().getXml(
R.color.text_color_green);
ColorStateList colors = ColorStateList.createFromXml(
getResources(), parser);
image.setTextColor(colors);
} catch (Exception e) {
}
//setting this value to -ve decreases the
//gap between the image and text
image.setCompoundDrawablePadding(-100);
image.setCompoundDrawablesWithIntrinsicBounds(
null,
getResources().getDrawable(
R.drawable.sidemenu_image_festout), null, null);
image.setBackgroundResource(R.drawable.button_background_green);
}
Here is how the image looks now..
I want to set padding on TOP of the image. So that the image doesn't look to close to the border of the button. How can I achieve this?
I have tried setting image.setPadding(0,10,0,0) but no effect.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
use this
I am making a puzzle game for which I have to display 16 images (4 X 4) on the screen at the same time. I am trying to set the height and width of images but no value of hieght and width is changing the image size. Moreover only 4 images are appearing instead of 16 images. I am using the folloing code to display images:
public void display()
{
LinearLayout llMain = new LinearLayout(this);
for(int i=0;i<4;i++)
{
LinearLayout llRow = new LinearLayout(this);
for(int j=i*4;j<tiles.length/4;j++)
{
ImageView iv = new ImageView(this);
iv.setImageBitmap(tiles[j]);
iv.setAdjustViewBounds(true);
iv.setMaxHeight(tileHeight);
iv.setMaxWidth(tileWidth);
iv.setMinimumHeight(tileHeight);
iv.setMinimumWidth(tileWidth);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(params);
llRow.addView(iv);
}
llMain.addView(llRow);
}
setContentView(llMain);
}
Can somebody please tell me that What am I doing wrong?
Thanks in advance
Try using the layout_weight attribute of LinearLayout to divide the screen into the parts you need. The ratio of the values you set to the different parts makes the parts become bigger or smaller.
I am programatically adding ImageView elements into a horizontal Linear Layout. Then I set the scaleX and scaleY properties to "2" on one of the ImageView resources. The image gets scaled properly, but it doesn't move the other ImageView elements, instead of that it overlaps them. I don't like the image to overlap with other images. How can I fix that? Here's my code:
int resources[] = {R.drawable.desert, R.drawable.koala, R.drawable.jellyfish,
R.drawable.lighthouse, R.drawable.desert};
for(int i=0; i<5; i++) {
ImageView logo = new ImageView(this);
logo.setLayoutParams(new LinearLayout.LayoutParams(100, 75));
logo.setImageResource(resources[i]);
logosContainer.addView(logo);
}
ImageView middleImage = (ImageView) logosContainer.getChildAt(2);
middleImage.setScaleX(middleImage.getScaleX() * 2);
middleImage.setScaleY(middleImage.getScaleY() * 2);
The result from the code looks like this:
http://imageshack.us/a/img15/2811/scaleal.jpg
You can clearly see that the scaled image overlaps with the other images.
Your ImageViews are already measured and placed in the container when you leave the loop. You would have to refresh your layout to make this work, which is tricky and often leads into "removing all views and adding them again". So why don't you just do the "scaling" in the loop?
for(int i=0; i<5; i++) {
ImageView logo = new ImageView(this);
if(i==middleImageIndex){
logo.setLayoutParams(new LinearLayout.LayoutParams(100*2, 75*2,1));
} else {
logo.setLayoutParams(new LinearLayout.LayoutParams(100, 75,1));
}
logo.setImageResource(resources[i]);
logosContainer.addView(logo);
}
The last parameter (100,75, 1) is the weight of the view, which makes sure that each ImageView is equally important and doesn't get overlapped.
Another note: setScale requires API level 11 or higher, which could cut a lot of users out there
I want to put 8 image thumbs in one horizontal line, using the whole available width.
The images are retrieved from a webservice which lets me specify the dimensions.
I tried the following:
int widthPx = container.getWidth();
LinearLayout thumbs = (LinearLayout)curView.findViewById(R.id.thumbs);
for(int i=0; i<pics.length; i++) {
ImageView iv = new ImageView(mContextt);
int thumbSize = widthPx / 8;
try {
String url = "http://someurl/" + pics[i] + "&width=" + thumbSize + "&height=" + thumbSize;
URL imgUrl = new URL(url);
Drawable imgD = Drawable.createFromStream(imgUrl.openStream(), "src");
iv.setImageDrawable(imgD);
}
catch (Exception e) {
Log.e(TAG, "loading image failed");
e.printStackTrace();
}
thumbs.addView(iv);
}
The LinearLayout thumbs has android:layout_width="fill_parent set. The thumbs produced by this code are significantly smaller then 1/8 of the width. Why is this? What would be the correct way?
Update:
This code is inside onCreateView of a Fragment. While my width value is calculated based on the root-view and is correct, thumbs.getWidth() returns 0, although the view is inflated before and should also have a width of 480 because of layout_width is set to fill_parent. I'm not sure if that's a problem.
Is my assumption correct that the layout of the created ImageViews is set to wrap_content by default? If not, how to set this with Java code?
Add the ImageViews with a fixed size, i.e.:
thumbs.addView (iv, new LayoutParams(thumbSize, thumbSize));
To answer (partially) the questions in the comments:
The ImageView API says:
takes care of computing its measurement from the image so that it can be used in any layout manager
so it is probably assuming 60px for a 160 dpi (I may be wrong there).
I'd suggest using this:
widthPx = getWindowManager().getDefaultDisplay().getWidth();