Android应用程序中的介绍图像

时间:2015-06-02 16:40:40

标签: android

如何在第一次启动Android应用程序时进行显示介绍图像的活动?

我有4张描述该应用的图片。所以我希望这些图片在应用首次运行时显示,用户可以从一个图像滑动到下一个图像,以便取消阻止应用的工作部分。

3 个答案:

答案 0 :(得分:2)

为了检查app是否第一次使用SharedPreferences并显示图像,你必须使用Bitmap,因为没有它你会得到内存错误。

在您的活动类中添加此代码。(不在onCreate方法中)

remove_tree "$tmpdir";

在onCreate方法中检查应用程序是否第一次启动并将图像添加到imageView小部件。

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

答案 1 :(得分:0)

首次访问教程时在sharedPreferences中保存标志,然后在启动时检查它。

If no flag: 
LauncherActivity -> TutorialActivity (shows four images) save flag -> MainActivity

If flagged:
Launcher Activity -> MainActivity

检查android dev指南以获取sharepreferences帮助。

另外我不确定你在安装过程中表达的意思。如果你的意思是在加载过程中,那么只需要在加载过程中显示图像?

答案 2 :(得分:0)

这叫做Appintro。这是第一次在应用启动时运行

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    public boolean isFirstStart;
    Context mcontext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                //  Intro App Initialize SharedPreferences
                SharedPreferences getSharedPreferences = PreferenceManager
                        .getDefaultSharedPreferences(getBaseContext());

                //  Create a new boolean and preference and set it to true
                isFirstStart = getSharedPreferences.getBoolean("firstStart", true);

                //  Check either activity or app is open very first time or not and do action
                if (isFirstStart) {

                    //  Launch application introduction screen
                    Intent i = new Intent(MainActivity.this, MyIntro.class);
                    startActivity(i);
                    SharedPreferences.Editor e = getSharedPreferences.edit();
                    e.putBoolean("firstStart", false);
                    e.apply();
                }
            }
        });
        t.start();

    }
}

http://www.viralandroid.com/2016/10/android-appintro-slider-example.html

http://www.androidhive.info/2016/05/android-build-intro-slider-app/

https://github.com/apl-devs/AppIntro

enter image description here