当我滚动图像时如何处理会滞后?

问题描述 投票:1回答:2

[我在tab1相对布局中使用android studio创建了一个应用,我有8张图像,但是当我运行我的应用并滚动时,它滞后了如何解决滞后问题。

这里是代码

package com.example.drh3cker.ihebski_swip;


import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

//this project developped by iheb ben salem@IBSSoft
public class MainActivity extends FragmentActivity {

ViewPager pager;
PagerTabStrip tab_strp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_tab);
    ma_pager_adapter mapager=new ma_pager_adapter(getSupportFragmentManager());
    pager=(ViewPager)findViewById(R.id.pager);

    pager.setAdapter(mapager);
    tab_strp=(PagerTabStrip)findViewById(R.id.tab_strip);
    tab_strp.setTextColor(Color.WHITE);
 //   tab_strp.setTextSize(14,14);
   // tab_strp.setTabIndicatorColor(Color.WHITE);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.share) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public void browser1(View view) {
    Intent browserIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com"));
    startActivity(browserIntent);

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:weightSum="1">

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/imageView"
        android:src="@drawable/fra"
        android:background="#f44437"
        android:contentDescription="@string/App_Title_Image" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:id="@+id/imageView2"
        android:src="@drawable/saavnmusicapp"
        android:paddingTop="8sp"
        android:onClick="browser1"
        android:contentDescription="@string/saavn_music_app"
        android:scaleType="fitCenter"
        android:layout_below="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        style="?android:borderlessButtonStyle"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:id="@+id/imageView3"
        android:src="@drawable/earntalktime"
        android:layout_below="@+id/imageView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/earn_talktime_image"
        android:onClick="browser2"
        style="?android:borderlessButtonStyle"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:id="@+id/imageView4"
        android:src="@drawable/flipkart"
        android:onClick="browser3"
        android:contentDescription="@string/flipkart_app_image"
        android:layout_below="@+id/imageView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        style="?android:borderlessButtonStyle"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:id="@+id/imageView5"
        android:layout_below="@+id/imageView4"
        android:layout_centerHorizontal="true"
        android:src="@drawable/paytmapp"
        android:contentDescription="@string/paytm_app_image"
        android:onClick="browser4"
        style="?android:borderlessButtonStyle"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:id="@+id/imageView6"
        android:layout_below="@+id/imageView5"
        android:layout_centerHorizontal="true"
        android:src="@drawable/uc"
        android:onClick="browser5"
        style="?android:borderlessButtonStyle"
        android:contentDescription="@string/uc_browser_image" />

    <ImageView
         android:layout_width="match_parent"
         android:layout_height="150dp"
         android:id="@+id/imageView7"
         android:layout_below="@+id/imageView6"
         android:layout_centerHorizontal="true"
         android:src="@drawable/dailyhunt"
         android:onClick="browser6"
         style="?android:borderlessButtonStyle"
         android:contentDescription="@string/uc_browser_image" />

    <ImageView
         android:layout_width="match_parent"
         android:layout_height="150dp"
         android:id="@+id/imageView8"
         android:layout_below="@+id/imageView7"
         android:layout_centerHorizontal="true"
         android:src="@drawable/freepaisa"
         android:onClick="browser7"
         style="?android:borderlessButtonStyle"
         android:contentDescription="@string/uc_browser_image" />

    <ImageView
         android:layout_width="match_parent"
         android:layout_height="150dp"
         android:id="@+id/imageView9"
         android:layout_below="@+id/imageView8"
         android:layout_centerHorizontal="true"
         android:src="@drawable/mobikwik"
         android:onClick="browser8"
         style="?android:borderlessButtonStyle"
         android:contentDescription="@string/uc_browser_image" />




</RelativeLayout>
</ScrollView>

Stackoverflow告诉我添加更多详细信息,我不知道为什么我添加了问题和代码,并且我想知道如何解决可以帮助任何人的问题

谢谢。

java android xml
2个回答
1
投票

一个更好的选择是使用Listview或什至更好的RecyclerView来代替ScrollView中的许多图像,因为它们可以回收未看到的项目,从而提高性能。 +毕加索(图像处理和缓存)。 https://github.com/square/picasso

以下是实现示例:https://futurestud.io/blog/picasso-adapter-use-for-listview-gridview-etc/

来自Picasso GitHub的代码示例:https://github.com/square/picasso/tree/master/picasso-sample/src/main/java/com/example/picasso

但是有很多很好的实现资源,只是谷歌-android ListView或Recycler view实现。

如果您仍然想使用ScrollView,则可以用Linearlayout替换RelativeLayout,这可能会带来很小的性能提升,因为RelativeLayout被测量了2次。


1
投票

在ImageView中使用时加载图像时

imageview.setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.image);

than

imageview.setImageResource(R.drawable.image);

根据我的经验,它最多可将Java和Graphics的内存消耗减少89%,这使应用程序运行更流畅,使用的内存更少,并且在应用程序运行时减少了垃圾回收。

下一步,对图像的文件大小设置限制,以确保可能的最佳性能

来自:image.jpg 1.2MB

To:.image.jpg 512KB

如果文件已经在资源文件夹中,则可以在Mac中“在Finder中显示”或在Windows中“在资源管理器中打开”以显示文件,然后可以...

  • 在Mac版Preview中调整图像大小
  • 使用Microsoft Office Picture Manager在Windows中调整图像大小或调整图像文件大小(即使在今天仍然有用)

最后,也许您会发现本文对您的问题有所帮助

https://medium.freecodecamp.org/how-we-reduced-memory-footprint-by-50-in-our-android-app-49efa5c93ad8


0
投票

就我而言,我将所有图像放在可绘制文件夹中,插入了可绘制xxxhdpi文件夹,这就是我的屏幕UI落后的原因。

© www.soinside.com 2019 - 2024. All rights reserved.