1
I’m using the drawable batch to generate different images according to dpi. Each original image has a resolution of 1920x1080 with an average size of 350kb. However, an image with a huge size of 4mb is generated in the xxxhdpi folder. (Is this normal? or should I use smaller original images?)
The problem is that it seems that the application takes a while to open the Fragment that contains the list of images. To try to solve this, I used Picasso. However, although now I can open Ragment quickly, the images take a while to load and this happens whenever I leave and come back from the same.
Would this be a problem in the cache or the fact that I am using images with a high resolution?
Custom Listview Adapter:
public class CustomListaImagens extends BaseAdapter {
String[] tituloServico;
Integer[] images;
Context context;
public CustomListaImagens(String[] tituloServico, Integer[] images, Context context) {
this.tituloServico = tituloServico;
this.images = images;
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View rowView = inflater.inflate(R.layout.lista_imagens_layout, parent, false);
TextView textView = rowView.findViewById(R.id.servico_titulo_id);
ImageView imageView = rowView.findViewById(R.id.imagem_servico_id);
textView.setText(tituloServico[position]);
imageView.setImageResource(images[position]);
return rowView;
}
}
Layout of the XML Custom Image List:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false">
<android.support.constraint.ConstraintLayout
android:id="@+id/container_lista_id"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@color/backgroundColor"
android:elevation="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:targetApi="lollipop">
<ImageView
android:id="@+id/imagem_servico_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/background1" />
<TextView
android:id="@+id/servico_titulo_id"
fontPath="fonts/Roboto-Regular.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@color/backgroundText"
android:elevation="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="BELEZA"
android:textColor="@color/Texto"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="@+id/imagem_servico_id"
app:layout_constraintEnd_toEndOf="@+id/imagem_servico_id"
app:layout_constraintStart_toStartOf="@+id/imagem_servico_id"
app:layout_constraintTop_toTopOf="@+id/imagem_servico_id"
tools:ignore="MissingPrefix"
tools:targetApi="lollipop" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Main activity where Fragments are loaded:
public class MainActivity extends AppCompatActivity {
private BottomNavigationViewEx bottomNavigationViewEx;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = ConfiguracaoFirebase.getFirebaseAutenticacao();
bottomNavigationViewEx = findViewById(R.id.bottom_nav);
BottomNavConfig.ConfigurarBottomNav(bottomNavigationViewEx);
bottomNavigationViewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
android.support.v4.app.Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.menu_pedidos:
selectedFragment = new PedidosFragment();
break;
case R.id.menu_home:
selectedFragment = new HomeFragment();
break;
case R.id.menu_conta:
selectedFragment = new MinhaContaFragment();
break;
case R.id.menu_help:
selectedFragment = new AjudaFragment();
break;
case R.id.menu_procurar:
selectedFragment = new ProcurarFragment();
break;
}
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (selectedFragment != null) {
fragmentTransaction.replace(R.id.frameLayout, selectedFragment);
fragmentTransaction.commit();
}
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
bottomNavigationViewEx.setSelectedItemId(R.id.menu_home);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
public void IrParaLoginActivity() {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}
}
Fragment containing the image list:
public class HomeFragment extends Fragment {
List<Address> addresses;
private Location location;
private TextView tv_location;
private String[] permissoesNecessarias = new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
};
private String[] TituloServico = {"BELEZA", "CASA", "INFORMATICA"};
private Integer[] imagens = {R.drawable.beleza, R.drawable.casa, R.drawable.informatica};
public HomeFragment() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
Permissao.validaPermissoes(1,getActivity(), permissoesNecessarias);
Toolbar toolbar = view.findViewById(R.id.toolbar_home);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
((MainActivity) Objects.requireNonNull(getActivity())).setSupportActionBar(toolbar);
}
ListView listView = view.findViewById(R.id.lista_imagens_id);
CustomListaImagens listaImagens = new CustomListaImagens(TituloServico, imagens, getActivity());
listView.setAdapter(listaImagens);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (TituloServico[position] == "CASA") {
startActivity(new Intent(getActivity(), CasaActivity.class));
}
}
});
tv_location = view.findViewById(R.id.tv_location_id);
pegaLocalizacao();
return view;
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for(int resultado : grantResults) {
if (resultado == PackageManager.PERMISSION_DENIED) {
alertaValidacaoPermissao();
}
}
}
public void alertaValidacaoPermissao() {
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
builder.setTitle("Permissões Negadas");
builder.setMessage("Para utilizar esse app é necessário aceitar as permissões");
builder.setPositiveButton("CONFIRMAR", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
finalize();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
});
android.support.v7.app.AlertDialog dialog = builder.create();
}
public void pegaLocalizacao() {
try {
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String cityName = addresses.get(0).getLocality();
tv_location.setText(cityName);
}
}