android: (bad)low image quality in layout background - android

I tried all the answers that i found in the Android section with out any success before i post this question...
for some reason the image quality in the device is poor and in Eclipse and in the virtual device is very good
look on the screenshot example:
example http://img714.imageshack.us/img714/1358/84044294.jpg
what should i do?
i try to make the image with 72dpi and 300 dpi, the PNG/JPG resolution is 1280x800
but nothing works...
Please help!!
This is my LiveNewsActivity.java maybe i'm doing something wrong?
package com.prog.livenews;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class LiveNewsActivity extends Activity {
/** Called when the activity is first created. */
//############################
//######### VARS #############
Button login;
Button register;
//############################
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap gradient = BitmapFactory.decodeResource(getResources(), R.drawable.bg, options);
findViewById(R.id.frameLayout1).setBackgroundDrawable(new BitmapDrawable(gradient));
login = (Button) findViewById(R.id.main_login_button);
register = (Button) findViewById(R.id.main_register_button);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.prog.livenews.LOGIN"));
}
});
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.prog.livenews.REGISTER"));
}
});
}
}

try it:
put it before setContentView(layoutId) on your first activity
getWindow().setFormat(PixelFormat.RGBA_8888);
and after call setContentView(layoutId) in your first activity you put the bellow code:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = true;
options.inScaled = false;
options.inDither = false;
options.inPurgeable = true;
Bitmap preparedBitmap = BitmapFactory.decodeResource(yourAppName.getSharedApplication().getResources(),
R.drawable.bg, options);
Drawable background = new BitmapDrawable(preparedBitmap);
((LinearLayout) findViewById(R.id.yourLayoutId))
.setBackgroundDrawable(background);
and finnaly you put your xml images in res/drawable "if you have xml images" folder and the png, jpeg and others normals images you put in res/drawable-hdpi folder.
Hope this helps.

I tried all the answers that i found in the Android section with out any success
It'd be helpful if you stated which approaches you've tried too, but Marc's comment is correct. You need to make sure your image and window are both higher bit depth (probably ARGB_8888). See this article for more info and you can possibly enable dithering. Putting this in onCreate should take care of a lot:
getWindow().setFormat(PixelFormat.RGBA_8888);

O.K guys, I found a solution...
As i understand... when saving the gradient image in Photoshop, Photoshop will try to optimize the image size by removing the Alpha channel.
So you need to delete one pixel from the top (left/right) corner or the buttom (right/left) corner
and then Photoshop will "see" that the image have an Alpha channel and it will work on the Device...
be aware... some devices are not able to show gradient color perfect..
Hope that was helpful! Thanks guys for your help!

Try using the High Quality Image. Can you confirm whether background is also containing the Globe Image and text on it? Is background is having gradient color or not ?

Related

GIF Listener in android

i'm using this How to animate .gif images in an android? solution (first answer of Shobhit Puri) to implements in my app some Gif. now i want to add some Listener on them (click listner).
every single procedure i tried didn't worked.
the idea is:
the gif is made by 3 image, when you click the first image the gif will change in another one, and so on.
Thanks for your attention.
EDIT:
actualy i'm trying this way
findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
}
});
on onCreate() of the mainActivity. the Log is well generated but i can't go over this
I'm not sure about gifs, but if you have separate files, you could do something like this.
Warning: Untested :)
final int[] imgs = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int position = 0;
final ImageView gifView = (ImageView) findViewById(R.id.ivAnimation);
gifView.setImageResource(imgs[position]);
gifView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
position = (position + 1) % imgs.length; // wrap around to "restart" gif
gifView.setImageResource(imgs[position]);
}
});

Change between images with a Button

I'm new to Android and I'm trying to change the content of an ImageView with a button and if I press the button again the image changes back. I thought it would be easy with an if-else statement but I have been looking around in the ImageView API and I don't see the method that allows me to get the image that is being displayed in that moment... Any ideas?
Here is my code so far...
boton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imagen.setImageResource(R.mipmap.imag1);
}
});
I didn't copy the rest of the code because I dont think it's necessary
You can get the image of the button with using this method:
Bitmap bitmap = ((BitmapDrawable)imagen.getDrawable()).getBitmap();
To set image of a button with another bitmap you can use:
imagen.setImageBitmap(bitmap);

Imageview to show different image depending on Int [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a random number generator and I need to display different image,
depending on different Int values.
For example when randomNumber is let's say 1, I need to show specific text and image
and when number is 10 another specific text and image etc..
Can I even do that with ImageView, I do not know where to start?
package com.example.drinktivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button back = (Button) findViewById(R.id.buttonback); // Here the R.id.button1 is the button from you design
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
}
});
ImageView image = (ImageView) findViewById(R.id.imageView1);
Bundle bundle = getIntent().getExtras();
int random = bundle.getInt("Value");
TextView text = (TextView) findViewById(R.id.textView1);
if (random==1) {
text.setText("");
}
if (random==2) {
text.setText("");
}
if (random==3) {
text.setText("");
}
if (random==4) {
text.setText("");
}
if (random==5) {
text.setText("");
}
if (random==6) {
text.setText("");
}
if (random==7) {
text.setText("");
}
if (random==8) {
text.setText("");
}
if (random==9) {
text.setText("");
}
if (random==10) {
text.setText("");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Create a drawable array
public static final int[] im_smiley_drawable_smile_old = {
R.drawable.im_smiley_happy, R.drawable.im_smiley_sad,
R.drawable.im_smiley_winking,
R.drawable.im_smiley_tongue_sticking_out,
R.drawable.im_smiley_surprised, R.drawable.im_smiley_kissing,
R.drawable.im_smiley_yelling, R.drawable.im_smiley_cool}
send ramdom numbers to this array you will get random images
int[] i = {R.drawable.drawable1, R.drawable.drawable2, R.drawable.drawable2, R.drawable.drawable4, R.drawable.drawable5,};
Random rand = new Random();
int n = rand.nextInt(5);
imageView.setBackground(getResources().getDrawable(i[n]));
consider your int value as int random
and your random no consists of numbers 1 to 5.
then you can do this.
if(random==1){
imageview.setImageBitmap(yourbitmap);
//here imageview is your imageview. and then bitmap you want when the number generated is 1.
}
This is definitely possible, but there are a lot of ways of doing this. I think the main approach that you'd want to go for is generating a random number, and choosing an image from your resources based on that number. You can retrieve resources like this:
context.getResources().getIdentifier(i.getIcon(), "drawable", context.getPackageName())

How to move a image View left and right in android development

I am very new to Android Development, and I am trying to develop a game. In this game, I will require a Imageview to move left and right when pressed by a button. My current IDE that I am using is "Android Studio". So I have done research but am not finding any answers. My current code is
package com.example.turtle;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
Button left, right ;
ImageView turtle ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
left = (Button) findViewById(R.id.bl);
right = (Button) findViewById(R.id.br);
turtle = (ImageView) findViewById(R.id.IVT);
left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
So yeah, As you can see I set up an OnClcikListener but I don't know what goes under it. I heard I can lessen a position, but how would I do that ? Like what is the code to lessen a position ? Thanks
At least for the newer Android studio this is how I would do it.
In your xml text code,(in your design/layout page go on the bottom and click text)
<Button
android:onClick="LeftBonClick"
(plus any additional xml code you have for this button.)
/>
And then on you java code...
public void LeftBonClick(View view) {
turtle.setX(turtle.getX() - 2); //<<code depending on your preference and what layout your turtle is in.
//add any code you want to happen here, when the left button is clicked
}
You can set the new parameteres for your ImageView with the following code inside onClick() method:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(50,50, 0,0);
// OR
params.topMargin= 50;
turtle.setLayoutParams(params);
Hope that will help you.

How Do I Load Images From SD Card One At A Time?

Ok, I am able to load a specific image into ImageView from my SD card...no problem. But I need to take a step further and add a button load the next image (move/copy/delete previous image). I can add the button, etc but how do I retreive the 'next' image without knowing filenames? Here is the code so far. I would have liked to use Gallery but it has been deprecated and I can't seem to make anything else work. Thank You
package com.demo.ShowSDImages;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class AndroidBitmap extends Activity
{private final String imageInSD = "/sdcard/er.PNG";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
myImageView.setImageBitmap(bitmap);
}
}
i think you can give a try to this sample here..Check this link
here i think the below code can help you in getting the images .
int imageCount = sdcardPath.listFiles().length;
for (int count = 0; count < imageCount - 1; count++) {
Bitmap bmp = BitmapFactory.decodeFile(sdcardPath.listFiles()[count]
.getAbsolutePath());
In the sample instead of using the thread or timer use the the same logic that is using of the showNext() in the button click event
Give a try. Happy Coding

Categories

Resources