Как использовать большое количество ImageView
на одном экране?
Экран моей активности:
У меня есть этот тип макета, каждое изображение отдельно ImageView
.
На некоторых устройствах он выдает OutOfMemoryError
, однако есть такие устройства, где он работает отлично.
Одно из "решений", которое я нашел в стеке:
скопируйте все содержимое папки drawable в drawable-xhdpi, drawable-xxhdpi и т. д., в течение некоторого времени это не давало никаких ошибок OutOfMemoryError
. Однако общий размер моего приложения увеличился на несколько МБ.
Я не уверен в подходе к обработке растровых изображений.
Вот код моего макета:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/mainbg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:orientation="vertical">
<ImageView
android:id="@+id/livingwall"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/bglivingback"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ImageView
android:id="@+id/windowliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:background="@drawable/windowliving"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline5"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline13" />
<ImageView
android:id="@+id/bagetpower"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/livingbaget"
app:layout_constraintBottom_toTopOf="@+id/guideline13"
app:layout_constraintEnd_toStartOf="@+id/guideline18"
app:layout_constraintStart_toStartOf="@+id/guideline17"
app:layout_constraintTop_toTopOf="@+id/guideline19" />
<ImageView
android:id="@+id/curtainliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="-8dp"
android:background="@drawable/curtainliving"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toStartOf="@+id/guideline22"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline19" />
<ImageView
android:id="@+id/wallclock"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/clockliving"
app:layout_constraintBottom_toTopOf="@+id/guideline12"
app:layout_constraintEnd_toStartOf="@+id/guideline16"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline15" />
<ImageView
android:id="@+id/wallpicture"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/wallpicture"
app:layout_constraintBottom_toTopOf="@+id/guideline12"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline13" />
<ImageView
android:id="@+id/bookman"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/bookshelfliving"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toStartOf="@+id/guideline14"
app:layout_constraintStart_toStartOf="@+id/guideline16"
app:layout_constraintTop_toTopOf="@+id/guideline15" />
<ImageView
android:id="@+id/floorliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#596c64"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<ImageView
android:id="@+id/carpetliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/carpetlivingroom"
app:layout_constraintBottom_toBottomOf="@+id/floorliving"
app:layout_constraintEnd_toEndOf="@+id/floorliving"
app:layout_constraintStart_toStartOf="@+id/floorliving"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<ImageView
android:id="@+id/armchairliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/armchairliving"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<ImageView
android:id="@+id/chestliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/chestliving"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline5"
app:layout_constraintHorizontal_bias="0.604"
app:layout_constraintStart_toEndOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline7" />
<ImageView
android:id="@+id/tvliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/tvliving"
app:layout_constraintBottom_toTopOf="@+id/guideline26"
app:layout_constraintEnd_toStartOf="@+id/guideline5"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline12"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="@+id/sofaliving"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/sofaliving"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline8"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<LinearLayout
android:id="@+id/extradetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/carpetliving"
app:layout_constraintEnd_toEndOf="@+id/livingwall"
app:layout_constraintStart_toStartOf="@+id/livingwall"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eee">
<Button
android:id="@+id/soundplayer"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/soundicon"
android:text="" />
<TextView
android:id="@+id/langrewwrtie"
style="@style/rooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:enabled="true"
android:text="langrew" />
<TextView
android:id="@+id/translation"
style="@style/rooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:enabled="true"
android:text="Translation" />
<TextView
android:id="@+id/howtoread"
style="@style/rooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:enabled="true"
android:text="Howtoread" />
<Button
android:id="@+id/closeit"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/closeicon"
android:text="" />
</LinearLayout>
</LinearLayout>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.79" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.31" />
<android.support.constraint.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.52" />
<android.support.constraint.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.86" />
<android.support.constraint.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.4703125" />
<android.support.constraint.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.67" />
<android.support.constraint.Guideline
android:id="@+id/guideline8"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.96" />
<android.support.constraint.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.52" />
<android.support.constraint.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.04" />
<android.support.constraint.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<android.support.constraint.Guideline
android:id="@+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
<android.support.constraint.Guideline
android:id="@+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.14" />
<android.support.constraint.Guideline
android:id="@+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.9867188" />
<android.support.constraint.Guideline
android:id="@+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2338377" />
<android.support.constraint.Guideline
android:id="@+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.64" />
<android.support.constraint.Guideline
android:id="@+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.29" />
<android.support.constraint.Guideline
android:id="@+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.484375" />
<android.support.constraint.Guideline
android:id="@+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.11" />
<android.support.constraint.Guideline
android:id="@+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.3765625" />
<android.support.constraint.Guideline
android:id="@+id/guideline23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="801dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="509dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.70321363" />
Вот моя ява:
import android.app.ProgressDialog;
import android.app.ActionBar;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import static android.content.Context.MODE_PRIVATE;
/**
* Created by ImRock on 2/19/2018.
*/
public class RoomLiving extends Fragment implements View.OnClickListener {
public RoomLiving(){}
private InterstitialAd mInterstitial;
String jsonString;
String[] names;
String current;
TypedArray soundslist;
private boolean hiddingdetails = true;
int where;
int hero;
int taps=0;
InputStream is;
private MediaPlayer mMediaPlayer;
Context context;
String woot;
private Handler mHandler = new Handler();
TextView hebrewwrite, translation, howtoread;
ImageView livingwall, windowliving,curtainliving, wallclock, bookshelf, wallpicture, floorliving, carpetliving, armchairliving, chestliving, tvliving, sofaliving;
Button soundplayerb, closeitb;
LinearLayout extra;
String[] learning;
int[] sounds = new int [] {R.raw.wall, R.raw.window, R.raw.curtain, R.raw.clock, R.raw.bookshelf, R.raw.picture, R.raw.floor, R.raw.carpet, R.raw.armchair, R.raw.roomcabinet, R.raw.tv, R.raw.sofa};
@Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
View rootView = inflater.inflate(R.layout.roomliving, container, false);
//View view2 = getActivity().findViewById(R.id.tabs);
//View view = getActivity().findViewById(R.id.toolbar);
//view2.setVisibility(View.GONE);
//view.setVisibility(View.GONE);// View view = getActivity().findViewById(R.id.tabs);
//view.setVisibility(View.GONE);
Locale current = getResources().getConfiguration().locale;
if (String.valueOf(current).equals("ru_UA") || String.valueOf(current).equals("uk_UA") || String.valueOf(current).equals("ru_MD") || String.valueOf(current).equals("ru_RU") || String.valueOf(current).equals("ru_BY")){
InputStream isrus = getResources().openRawResource(R.raw.feeder);
Resources res = getResources();
jsonBuddy jb = new jsonBuddy(isrus);
jsonString = jb.getJsonString();
learning = res.getStringArray(R.array.room);
names = res.getStringArray(R.array.room);
soundslist = res.obtainTypedArray(R.array.roomsounds);
}
else {
InputStream iseng = getResources().openRawResource(R.raw.feedereng);
Resources res = getResources();
jsonBuddy jb = new jsonBuddy(iseng);
jsonString = jb.getJsonString();
learning = res.getStringArray(R.array.room);
names = res.getStringArray(R.array.itemsname);
soundslist = res.obtainTypedArray(R.array.soundslist);
}
extra = (LinearLayout)rootView.findViewById(R.id.extradetails);
hebrewwrite = (TextView)rootView.findViewById(R.id.hebrewwrtie);
translation = (TextView)rootView.findViewById(R.id.translation);
howtoread = (TextView)rootView.findViewById(R.id.howtoread);
livingwall = (ImageView)rootView.findViewById(R.id.livingwall);
windowliving = (ImageView)rootView.findViewById(R.id.windowliving);
curtainliving = (ImageView)rootView.findViewById(R.id.curtainliving);
wallclock = (ImageView)rootView.findViewById(R.id.wallclock);
bookshelf = (ImageView)rootView.findViewById(R.id.bookman);
wallpicture = (ImageView)rootView.findViewById(R.id.wallpicture);
floorliving = (ImageView)rootView.findViewById(R.id.floorliving);
carpetliving = (ImageView)rootView.findViewById(R.id.carpetliving);
armchairliving = (ImageView)rootView.findViewById(R.id.armchairliving);
chestliving = (ImageView)rootView.findViewById(R.id.chestliving);
tvliving = (ImageView)rootView.findViewById(R.id.tvliving);
sofaliving = (ImageView)rootView.findViewById(R.id.sofaliving);
soundplayerb = (Button)rootView.findViewById(R.id.soundplayer);
closeitb = (Button)rootView.findViewById(R.id.closeit);
livingwall.setOnClickListener(this);
windowliving.setOnClickListener(this);
curtainliving.setOnClickListener(this);
wallclock.setOnClickListener(this);
wallpicture.setOnClickListener(this);
floorliving.setOnClickListener(this);
carpetliving.setOnClickListener(this);
armchairliving.setOnClickListener(this);
chestliving.setOnClickListener(this);
tvliving.setOnClickListener(this);
sofaliving.setOnClickListener(this);
soundplayerb.setOnClickListener(this);
closeitb.setOnClickListener(this);
bookshelf.setOnClickListener(this);
return rootView;
}
public void extrainfo(final String curitem){
if(!hiddingdetails){
hiddingdetails = true;
extra.setVisibility(View.VISIBLE);
List<String> list = Arrays.asList(names);
if (list.contains(curitem)){
where = list.indexOf(curitem);
}
extra.animate().setDuration(400);
extra.animate().translationY(200);
mHandler.postDelayed(new Runnable() {
public void run() {
loadup(curitem);
showit();
}
}, 500);
}
else {
extra.setVisibility(View.VISIBLE);
hiddingdetails = false;
List<String> list = Arrays.asList(names);
if (list.contains(curitem)){
where = list.indexOf(curitem);
}
extra.animate().setDuration(400);
extra.animate().translationY(0);
loadup(curitem);
showit();
Log.e("wat", curitem + String.valueOf(where));
}
}
public void hideit(){
hiddingdetails = true;
extra.setVisibility(View.VISIBLE);
extra.animate().setDuration(400);
extra.animate().translationY(200);
}
public void showit(){
hiddingdetails = false;
extra.setVisibility(View.VISIBLE);
extra.animate().setDuration(400);
extra.animate().translationY(0);
}
private void loadup(String wordfind) {
try {
JSONObject jsonObj = new JSONObject(jsonString);
JSONArray contacts = jsonObj.getJSONArray("letters");
for (int i = 0; i < contacts.length(); i++){
JSONObject scan = contacts.getJSONObject(i);
String wordjson = scan.getString("word");
String wordlikerussian = scan.getString("wordlikerussian");
String wordinhe = scan.getString("wordinhe");
String word = scan.getString("word");
if (wordjson.equals(wordfind)){
hebrewwrite.setText(wordinhe);
translation.setText(current);
howtoread.setText(wordlikerussian);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void playingman (int l){
tapcounter();
mMediaPlayer = MediaPlayer.create(getContext(), sounds[l]);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
mediaPlayer.release();
}
});
mMediaPlayer.start();
}
public void tapcounter(){
taps++;
if (taps==10){
if(mInterstitial.isLoaded()){
mInterstitial.show();
}
else {
taps = 0;
}
}
}
}