Reduce memory setting image to imageview - android

I am showing a series of questions to the user. In each question I set drawables to 6 imageviews. The only problem is the ram usage goes up about 60MB ever time I go to the next screen.
I dont understand why this is happening because I am just replacing the picture with a different one? After about 5 screens the app crashing because of failed memory allocation. How would I release this memory of the screen before it so It doesnt build up.
Thanks in advance.
public class Test extends ActionBarActivity {
ImageButton a1;
ImageButton a2;
ImageButton a3;
ImageButton a4;
Intent result;
String correctOrWrong = "";
int question;
int amountOfC = 0;
int numCorrect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
getSupportActionBar().hide();
ImageView qMain = (ImageView) findViewById(R.id.qMain);
a1 = (ImageButton) findViewById(R.id.a1);
a2 = (ImageButton) findViewById(R.id.a2);
a3 = (ImageButton) findViewById(R.id.a3);
a4 = (ImageButton) findViewById(R.id.a4);
ImageView qTitle = (ImageView) findViewById(R.id.qTitle);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(1);
}
});
a2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(2);
}
});
a3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(3);
}
});
a4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextQ(4);
}
});
Intent qGet = getIntent();
question = qGet.getIntExtra("QNUMBER", 1);
numCorrect = qGet.getIntExtra("NUMCORRECT", 1);
Log.d("lel", String.valueOf(question));
if (question == 1) {
qMain.setBackgroundResource(R.drawable.q1);
a1.setBackgroundResource(R.drawable.a13);
a2.setBackgroundResource(R.drawable.a12);
a3.setBackgroundResource(R.drawable.a11);
a4.setBackgroundResource(R.drawable.ca1);
qTitle.setBackgroundResource(R.drawable.q1t);
} else if (question == 2) {
qMain.setBackgroundResource(R.drawable.q2);
a1.setBackgroundResource(R.drawable.q23);
a2.setBackgroundResource(R.drawable.q2a);
a3.setBackgroundResource(R.drawable.q22);
a4.setBackgroundResource(R.drawable.q21);
qTitle.setBackgroundResource(R.drawable.q2t);
} else if (question == 3) {
qMain.setBackgroundResource(R.drawable.q3);
a1.setBackgroundResource(R.drawable.q31);
a2.setBackgroundResource(R.drawable.q32);
a3.setBackgroundResource(R.drawable.q33);
a4.setBackgroundResource(R.drawable.q3a);
qTitle.setBackgroundResource(R.drawable.q3t);
} else if (question == 4) {
qMain.setBackgroundResource(R.drawable.q4);
a1.setBackgroundResource(R.drawable.q4a);
a2.setBackgroundResource(R.drawable.q43);
a3.setBackgroundResource(R.drawable.q42);
a4.setBackgroundResource(R.drawable.q41);
qTitle.setBackgroundResource(R.drawable.q4t);
} else if (question == 5) {
qMain.setBackgroundResource(R.drawable.q5);
a1.setBackgroundResource(R.drawable.q53);
a2.setBackgroundResource(R.drawable.q5a);
a3.setBackgroundResource(R.drawable.q52);
a4.setBackgroundResource(R.drawable.q51);
qTitle.setBackgroundResource(R.drawable.q5t);

don't handle image memory/caching yourself, instead use a library such as glide - https://github.com/bumptech/glide
or
try clearing the imageview onDestroy() of the acitvity
a1.setImageDrawable(null);
a2.setImageDrawable(null);
a3.setImageDrawable(null);
a4.setImageDrawable(null);

you need to call recycle() for every bitmap after you used them like below :
a1.setImageDrawable(null);
a2.setImageDrawable(null);
a3.setImageDrawable(null);
a4.setImageDrawable(null);
a1.recycle();
a2.recycle();
a3.recycle();
a4.recycle();

Related

how do I set up a next and previous button

Hello as the title state I'm trying to setup a next and previous buttons but I'm still new at coding so this has me a little confused.
I tried to use if statements with an enum within a single button but it defaults to last if statement when the event is handled here's the code-
private enum EVENT{
pe1, pe2, pe3, pe4;
}
EVENT currentEvent = EVENT.pe1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_liners);
nextBtn = (Button) findViewById(R.id.nextBtn);
olText = (TextView) findViewById(R.id.olText);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (currentEvent==EVENT.pe1) {
olText.setText("PE1");
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_left));
currentEvent=EVENT.pe2;
}
if (currentEvent==EVENT.pe2){
olText.setText("PE2");
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_left));
currentEvent=EVENT.pe3;
}
}
});
}
I tried to use the enumerator to assign a number to each if statement so when the user hit previous it would subtract and when they hit next it would add, each number would have some text or image within its if statement but as I said it defaults to the last if statement- Any help is much appreciated.
How about this?
int eventNum = 0;
int maxEvents = XXX;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_liners);
prevBtn = (Button) findViewById(R.id.prevBtn);
nextBtn = (Button) findViewById(R.id.nextBtn);
olText = (TextView) findViewById(R.id.olText);
setEventData(true);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(prevBtn) && eventNum > 0) {
eventNum--;
setEventData(false);
return;
}
if(v.equals(nextBtn) && eventNum < maxEvents - 1) {
eventNum++;
setEventData(true);
return;
}
}
}
nextBtn.setOnClickListener(listener);
prevBtn.setOnClickListener(listener);
}
private void setEventData(boolean animLeft) {
olText.setText("PE" + (eventNum + 1));
if(animLeft) {
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_left));
} else {
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_right));
}
}
You'll want to create a class variable that keeps track of which text your TextView is showing. So in the following example, I create a list of Strings that I just store in a String array. Then I create an iterator variable which stores which String from the list I'm currently viewing in the TextView. Every time you click the previous or next button, you simply store your current state in the iterator variable so you can recall it the next time a click event comes in.
String[] labels = {"one", "two", "three", "four"};
int currentView = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onPreviousButtonClicked(View view) {
TextView textView = (TextView) findViewById(R.id.clickableLink);
currentView--; //decrement our iterator
if(currentView < 0) currentView = 0; //check to make sure we didn't go below zero
textView.setText(labels[currentView]);
}
public void onNextButtonClicked(View view) {
TextView textView = (TextView) findViewById(R.id.clickableLink);
currentView++; //increment our iterator
if(currentView > labels.length-1) currentView = labels.length-1; //check to make sure we didn't go outside the array
textView.setText(labels[currentView]);
}

View Image in One activity using SqlLite Database

i'm using SqlLite Database to store this image, then retrieve it.
If i'm click Next button the new image will be display, but the activity never change. (Sorry my English). image : http://www.pixentral.com/show.php?picture=101dALzioHZuNTHvJ3Vz9UBqk1NrlK0
--- EDITED ---
public class MainActivity extends Activity {
int imgNumber = 1;
...
ImageView imgView = (ImageView)findViewById(R.id.imageview);
imgView.setImageResource(R.drawable.image1);
...
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (imgNumber==0) {
imgView.setImageResource(R.drawable.image1);
imgNumber++;
} else if(imgNumber==1) {
imgView.setImageResource(R.drawable.image2);
imgNumber++;
} else if(imgNumber==2) {
imgView.setImageResource(R.drawable.image3);
imgNumber++;
} else if(imgNumber==3) {
imgView.setImageResource(R.drawable.image4);
imgNumber++;
} else if(imgNumber==4) {
imgView.setImageResource(R.drawable.image5);
imgNumber = 0;
}
}
});
...
}

How to change image next and previous using button clicklistener in android?

I have next and previous button for changing the image. When activity launch, image comes from previous activity. I use bundle object for getting image on my current activity. Actually 2 images use for pass it on bundle(image_a_inner and image_a_outer). One image overlap on second image and set on custom view. Now i want to when any image comes from bundle then i press next button or previous button then according to position image will be change. For example, images like A_Z alphabet. When i press on D image then it display on my activity using bundle and when i press next button then E image will be shown or when i press previous button then C image will be shown. Below is my code.
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int imageRes1 = extras.getInt("picture1");
int imageRes2 = extras.getInt("picture2");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(imageRes1, imageRes2);
btn_next = (Button) findViewById(R.id.btn_next);
// btn_next.setOnClickListener(this);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_a_inner||imageRes2==R.drawable.img_a){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index++;
}
else if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index++;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_d_inner, R.drawable.img_d);
index++;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_e_inner, R.drawable.img_e);
index++;
}
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
// btn_prev.setOnClickListener(this);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_a_inner, R.drawable.img_a);
index--;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index--;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index--;
}
});
}
If you have lists for all images in advance, declare them as arrays.
Otherwise, you can pass them using bundle with getIntArray() and putIntArray().
Now, you have lists of images like this,
// These are can be declared as member or static variables.
int[] innerPictures = {R.drawable.image_a_inner, R.drawable.image_b_inner, ...}
int[] pictures = {R.drawable.image_a, R.drawable.image_b, ...}
or
int[] innerPictures = extras.getIntArray("innerPictures");
int[] pictures = extras.getIntArray("pictures");
And you need the index of image to be displayed at the first time, it can be also passed as a extra
int displayingIndex = extra.getInt("pictureIndex"); // it has to be member variable to use inside of listener
So code is like below,
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int[] innerPictures = ...
int[] pictures = ...
displayingIndex = extra.getInt("pictureIndex");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex + 1 == innerPictures.length) return;
displayingIndex++;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex == 0) return;
displayingIndex--;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
});
}
Sorry for bad indentation.

Android how to call another Image after 3 times click on same Image?

I am developing application on Images.
In that if I click on same image the image will replace by another one that already into my Drawable
Right now onclick I am able to display only Toast Message; but I want to replace Image. I don't know How to do?
Any Help and suggestion appreciable.
you can take it images name as : img1 ,img2 ,img3.
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (v.isClickable()) {
i++;
String ii = new Integer(i).toString();
Log.i("Inside", ii);
if (ii.equals("3")) {
Toast.makeText(getApplicationContext(), "Call another Image ",Toast.LENGTH_SHORT).show();}
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setClickable(true);
int counter = 0;
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
i++;
if(i == 3){
imgV.setImageResource(R.drawable.img2);
//Or you may want to use
//imgV.setBackgroundResource(R.drawable.img2);
i = 0;
}
}

Android Button setAlpha

There are a set of buttons, I want to get the result:
When I click one of them, first I divide them into two parts: the clicked one and the others. I'm trying to set different color or alpha value to different them.
Now I use setAlpha, but when I change the value from 0 to 255, it works, but when I change the value from 255 to 0 , it doesnot work. I don't know why.
Maybe after I invoke the methodButton.setAlpha(), I need invoke another method?
my code:
public class MainActivity extends Activity {
// button alpha value: minimize value
public static int BUTTON_ALPHA_MIN = 0;
// button alpha value: maximize value
public static int BUTTON_ALPHA_MAX = 255;
private LinearLayout centerRegion;
private LinearLayout bottomRegion;
private Button btnCheckIn;
private Button btnReview;
private Button btnMyCircles;
private Button btnSettings;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get all the widgets
getAllWidgets();
// set buttons click response function
btnCheckIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.RED);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
}
});
btnReview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.BLUE);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
}
});
btnMyCircles.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.YELLOW);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MAX);
v.getBackground().setAlpha(BUTTON_ALPHA_MIN);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.MAGENTA);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MAX);
v.getBackground().setAlpha(BUTTON_ALPHA_MIN);
}
});
}
/**
* get all the widgets
*/
public void getAllWidgets() {
this.centerRegion = (LinearLayout) this.findViewById(R.id.center_region);
this.bottomRegion = (LinearLayout) this.findViewById(R.id.bottom_region);
this.btnCheckIn = (Button) this.findViewById(R.id.button_check_in);
this.btnReview = (Button) this.findViewById(R.id.button_review);
this.btnMyCircles = (Button) this.findViewById(R.id.button_my_circles);
this.btnSettings = (Button) this.findViewById(R.id.button_setting);
}
}
Using AlphaAnimation should work; verified on my device.
public class Test extends Activity implements OnClickListener {
private AlphaAnimation alphaDown;
private AlphaAnimation alphaUp;
private Button b1;
private Button b2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.linear_layout);
b1 = new Button(this);
b1.setText("Button 1");
b1.setOnClickListener(this);
ll.addView(b1);
b2 = new Button(this);
b2.setText("Button 2");
b2.setOnClickListener(this);
ll.addView(b2);
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(1000);
alphaUp.setDuration(1000);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
}
public void onClick(View v) {
if (v == b1) {
b1.startAnimation(alphaUp);
b2.startAnimation(alphaDown);
} else {
b1.startAnimation(alphaDown);
b2.startAnimation(alphaUp);
}
}
}
The key is calling setFillAfter(true) so that the alpha change persists.
Thanks for the question and the answer. This really helped me out.
For my solution, I needed to set the alpha of a button without seeing any animation effect, but the button.setAlpha(x) was failing sporadically. Using animations instead did the trick, but I had to set the duration to zero to get the automatic effect.
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(0);
alphaUp.setDuration(0);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
I use this for player controls in a media application, so I had something like this:
boolean bInitPrevEnabled = m_btPrev.isEnabled();
boolean bInitNextEnabled = m_btNext.isEnabled();
boolean bInitPlayEnabled = m_btPlay.isEnabled();
m_btPrev.setEnabled(true);
m_btNext.setEnabled(true);
m_btPlay.setEnabled(true);
// Process enabling of the specific buttons depending on the state
if (bInitPrevEnabled != m_btPrev.isEnabled())
m_btPrev.startAnimation((m_btPrev.isEnabled()) ? alphaUp : alphaDown);
if (bInitNextEnabled != m_btNext.isEnabled())
m_btNext.startAnimation((m_btNext.isEnabled()) ? alphaUp : alphaDown);
if (bInitPlayEnabled != m_btPlay.isEnabled())
m_btPlay.startAnimation((m_btPlay.isEnabled()) ? alphaUp : alphaDown);
Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.main_btn);
Drawable d = getResources().getDrawable(R.drawable.imagen);
d.setAlpha(60);
btn.setBackgroundDrawable(d);
}
This works for me :)
public void setAlpha (int alpha) - deprecated
public void setAlpha (float alpha) (0f < alpha < 1f)
Added in API level 11

Categories

Resources