问题描述
可能有人请帮我出了code,以随机显示从我的图纸文件夹中的照片吗?我是新来的Java,所以我不知道该怎么做。任何帮助将AP preciated。我的要求是: -1.显示随机图像(图像应更改每个启动)2.that是所有
导入了java.util.Random;
导入了java.util.Random;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.ImageView;
公共类MainMenu的延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main_menu);
}
公共无效openNewActivity(查看视图){
意向意图=新的意图(这一点,MainActivity.class);
startActivity(意向); //按钮打开主
}
公共无效浏览器(查看视图){
意向意图=新的意图(这一点,Browser.class);
startActivity(意向); //按钮打开浏览器
}
最后随机RND =新的随机();
{
的setContentView(R.layout.activity_main_menu);
最后ImageView的IMG =(ImageView的)findViewById(R.id.imgRandom);
//我有一个名为img_0到img_2 3张图片,所以...
最终的字符串str =IMG_+ rnd.nextInt(9);
img.setImageDrawable
(
getResources()。getDrawable(getResourceID(STR,绘制,
getApplicationContext()))
);
}
受保护的最后静态INT getResourceID
(最后弦乐resName,最后弦乐resType,最终上下文CTX)
{
最终诠释的ResourceID =
ctx.getResources()。则getIdentifier(resName,resType,
ctx.getApplicationInfo()的packageName)。
如果(的ResourceID == 0)
{
抛出新抛出:IllegalArgumentException
(
+ resName有没有相应的资源字符串中找到
);
}
其他
{
返回的ResourceID;
}
}
}
把一些图片命名img_0在你的资源来img_n /绘制文件夹
布局(RES /设计/ rnd_images.xml):
< RelativeLayout的
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=。MainActivity
>
< ImageView的
机器人:ID =@ + ID / imgRandom
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
/>
< / RelativeLayout的>
code:
包com.example.app;
导入了java.util.Random;
进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.widget.ImageView;
公共类MainActivity
扩展活动
{
最后随机RND =新的随机();
@覆盖
保护无效的onCreate(
最终捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.rnd_images);
最后ImageView的IMG =(ImageView的)findViewById(R.id.imgRandom);
//我有一个名为img_0到img_2 3张图片,所以...
最终的字符串str =IMG_+ rnd.nextInt(2);
img.setImageDrawable
(
getResources()。getDrawable(getResourceID(STR,绘制,
getApplicationContext()))
);
}
受保护的最后静态INT getResourceID
(最后弦乐resName,最后弦乐resType,最终上下文CTX)
{
最终诠释的ResourceID =
ctx.getResources()。则getIdentifier(resName,resType,
ctx.getApplicationInfo()的packageName)。
如果(的ResourceID == 0)
{
抛出新抛出:IllegalArgumentException
(
+ resName有没有相应的资源字符串中找到
);
}
其他
{
返回的ResourceID;
}
}
}
请注意,您必须设置rnd.nextInt(2)rnd.nextInt(最大值 - 1),因为RND从0开始
[更新]
布局名称必须与在的setContentView。
所以,如果你有(为什么?)这样的:
的setContentView(R.layout.activity_main);
在你MainActivity.java/onCreate,然后重命名布局activity_main.xml
,或者更好,使用我的code原样。
它的工作原理没有修改。
[更新]
检查这行:
最后随机RND =新的随机();
,以下导入:
导入了java.util.Random;
我的code 的工作原理是。我要你给它之前,测试它。
只需把我的RES /排版布局,RES /绘制和MainActivity.java图像来取代默认的。
请,请注意,图像名称必须是IMG_#,其中#是一个数字。
这个数字必须是0到(最大值 - 1)。
或者给喜欢my_city_#或其他名称。
但你必须更新Java code为匹配这些名字
Could someone please help me out with a code to randomly display a picture from the my drawings folder? i am new to java so i have no idea how to do it.Any help will be appreciated.My Requirements are:-1.Display Random image (image should change on each start up)2.that's all
import java.util.Random;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
}
public void openNewActivity(View view) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);//button to open main
}
public void browser(View view) {
Intent intent = new Intent(this,Browser.class);
startActivity(intent);//button to open browser
}
final Random rnd = new Random();
{
setContentView(R.layout.activity_main_menu);
final ImageView img = (ImageView) findViewById(R.id.imgRandom);
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(9);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
}
Put some images named img_0 to img_n in your res/drawable folder
Layout (res/layout/rnd_images.xml):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<ImageView
android:id="@+id/imgRandom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Code:
package com.example.app;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity
extends Activity
{
final Random rnd = new Random();
@Override
protected void onCreate(
final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rnd_images);
final ImageView img = (ImageView) findViewById(R.id.imgRandom);
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(2);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
}
Note that you have to set rnd.nextInt(2) to rnd.nextInt(Max - 1), since rnd starts from 0
[UPDATE]
The layout name must match that in setContentView.
So, if you have (why?) this:
setContentView(R.layout.activity_main);
in your MainActivity.java/onCreate, then rename the layout "activity_main.xml"
OR, better, USE MY CODE AS IS.
It works without modifications.
[UPDATE]
Check this line:
final Random rnd = new Random();
It requires the following import:
import java.util.Random;
My code works as is. I tested it before giving it to you.
Just place my layout in res/layout, the images in res/drawable and the MainActivity.java to replace the default one.
Please, notice that the images names MUST be "img_#" where # is a number.
This number must be 0 to (max - 1).
Or give names like "my_city_#" or whatever.
But then you must update the java code to match these names.
这篇关于如何显示在图像显示随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!