【问题标题】:How to display a random image for x second once the activity is first brought up?首次启动活动后,如何在 x 秒内显示随机图像?
【发布时间】:2013-04-20 06:26:43
【问题描述】:

当我的主要活动启动时,我试图显示一个随机图像 5 秒(我有 3 个图像)。 (这是一种如何使用我的应用程序和一些广告的教程)。但我只想每天显示一次。我需要使用 SharedPreferences 对吗?这是最好的方法,不是吗?所以我发现了这个:

ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(n) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
imgView.setImageResource(id); 

显示随机图像。还有这个:

public class mActivity extends Activity {
@Overrride
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setContentView(R.id.layout);

  // Get current version of the app
  PackageInfo packageInfo = this.getPackageManager()
      .getPackageInfo(getPackageName(), 0);
  int version = packageInfo.versionCode;

  SharedPreferences sharedPreferences = this.getPreferences(MODE_PRIVATE);
  boolean shown = sharedPreferences.getBoolean("shown_" + version, false);

  ImageView imageView = (ImageView) this.findViewById(R.id.newFeature);
  if(!shown) {
      imageView.setVisibility(View.VISIBLE);

      // "New feature" has been shown, then store the value in preferences
      SharedPreferences.Editor editor = sharedPreferences.edit();
      editor.put("shown_" + version, true);
      editor.commit();
  } else
      imageView.setVisibility(View.GONE);
}

在应用更新后显示应用的当前版本。我试图为我的应用程序调整这些代码,但我失败了。我还需要图像必须仅显示 5 秒并自动关闭。

嘿,又是我。我现在得到了这段代码,它运行良好:

boolean firstboot = getSharedPreferences("BOOT_PREF",MODE_PRIVATE).getBoolean("firstboot", true);
    getSharedPreferences("BOOT_PREF",MODE_PRIVATE).edit().
    putBoolean("firstboot", true).commit();

if(firstboot){
Intent webBrowser = new Intent(getApplicationContext(), WebBrowser.class);
// dismiss it after 5 seconds
    webBrowser.putExtra("url", "http://sce.jelocalise.fr/mobile/ajax/interstitiel.php");
    startActivity(webBrowser); 

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent MyIntent = new Intent(getApplicationContext(), Home.class);
            startActivity(MyIntent);
            }
        }
    }, 5000);

    getSharedPreferences("BOOT_PREF",MODE_PRIVATE).edit().
    putBoolean("firstboot", false).commit();
                         }

我现在想要的:我的 webview 上有一个取消按钮,当我点击它时,它会完成 webBrowser 活动。问题是当我单击取消按钮时,处理程序不会停止,并且在 5 秒后它会重新加载 Home 活动(我知道这是正常的)。我只希望取消按钮杀死处理程序。我已经尝试过 handler.removeCallbacks 方法,但我并不真正了解它是如何工作的。

【问题讨论】:

  • 嗨,在谷歌上搜索“闪屏”,我想这就是你要找的东西,你只需要检查一个偏好,它会显示最后一次显示的时间和今天的时间看看它今天是否已经可用
  • 您好,感谢您的帮助,但我已经得到了启动画面。我忘了说这个。在我的第三个活动(这是我的主要活动)中,我想展示一些我所做的解释和广告(因为我有其他产品)。
  • Thomas,您需要开始一个新问题。您现在所问的与您最初的问题完全无关。

标签: android image random


【解决方案1】:

好的,所以您想展示一张图片 5 秒钟,并且您不想每天展示该图片的次数超过一次?这意味着您需要跟踪上次显示图像的时间,SharedPreferences 对此非常有效。我建议您使用自定义 AlertDialog 来显示图像。它看起来不错,并且会使后台的活动变暗。我建议使用 Timer 和 TimerTask 在一段时间后关闭对话框。这是一个例子:

 /**
 * imageIds is an array of drawable resource id to chose from
 * Put the images you like to display in res/drawable-nodpi (if you
 * prefer to provide images for several dpi_bracket you put them in
 * res/drawable-mpdi, drawable-hdpi etc).
 * Add each of their resource ids to the array. In the example
 * below I assume there's four images named myimage1.png (or jpg),
 * myimage2, myimage3 and myimage4. 
 */
@Overrride
public void onCreate(Bundle savedInstanceState) {
    final int[] imageIds = { R.drawable.myimage1, R.drawable.myimage2, R.drawable.myimage3, R.drawable.myimage4 };
    final int id = new Random().nextInt(imageIds.length - 1);  
    showImageDialog(id, 24 * 60 * 60 * 1000, 5000);
}

/**
* Show an image in a dialog for a certain amount of time before automatically dismissing it
* The image will be shown no more frequently than a specified interval
* @param drawableId A resource id for a drawable to show
* @param minTime Minimum time in millis that has to pass since the last time an aimage was shown
* @param delayBeforeDismiss Time in millis before dismissing the dialog 
*
*/
private void showImageDialog(int drawableId, long minTime, long delayBeforeDismiss) {
    final SharedPreferences prefs = getPreferences(MODE_PRIVATE);
    // make sure we don't show image too often
    if((System.currentTimeMillis() - minTime) < prefs.getLong("TIMESTAMP", 0)) {
        return;
    }

    // update timestamp
    prefs.edit().putLong("TIMESTAMP", System.currentTimeMillis()).commit();

    // create a custom alert dialog with an imageview as it's only content
    ImageView iv = new ImageView(this);
    iv.setBackgroundDrawable(getResources().getDrawable(drawableId));       
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(iv);
    final AlertDialog dialog = builder.create();
    dialog.show();

    // dismiss it after 5 seconds
    new Timer().schedule(new TimerTask() {          
        @Override
        public void run() {
            dialog.dismiss();
        }
    }, delayBeforeDismiss);
}

【讨论】:

  • 谢谢我试试这个!只是为了确保此代码不会选择随机图像吗?不过,真的很大 thx !我会将 Rstar 的答案与您的答案结合起来,将“随机”的事情和“一天一次”的事情结合起来。
  • 正确。它不会在随机图像之间进行选择。我会更新我的答案。
  • 顺便说一句,选择随机图像的代码部分没有问题。我在更新的示例中保留了这一点。
  • 谢谢,我在理解这一行时遇到了问题: int id = getResources().getIdentifier("img" + rndInt, "drawable", getPackageName());我必须在“img”等中添加什么......以及这一行:showImageDialog(id,什么id?抱歉这些问题我对这一切都不是很熟悉。
  • 哦,对不起,getResources().getIdentifier("img" + rndInt);来自你自己的例子。我以为你明白它做了什么。我将再次更新我的示例。
【解决方案2】:

试试这个代码

public class MainActivity extends Activity {

Random random = new Random();
int max = 2;
int min = 0;

ImageView imageView;

Integer[] image = { R.drawable.ic_launcher, R.drawable.tmp,R.drawable.android };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    int randomNumber = random.nextInt(max - min + 1) + min;

    imageView = (ImageView) findViewById(R.id.img);

    imageView.setImageResource(image[randomNumber]);

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent intent = new Intent(MainActivity.this, Act.class);
            startActivity(intent);
        }
    }, 5000);
  }
}

【讨论】:

  • 好的,谢谢 Rstar,我会尝试用 britzl 的代码调整你的代码。再次,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多