基于定时器的动态背景(线性布局),如何实现?

发布于 2024-12-04 02:29:01 字数 1249 浏览 0 评论 0原文

嗨,我是 Android 操作系统编程的初学者,我遇到了一个问题,我不知道如何基于计时器制作动态背景(比如说每 10 秒背景就会更改为不同的背景)我有一些代码,但它出现错误,这里有一个示例:

private static final long GET_DATA_INTERVAL = 10000;
int images[] = {R.drawable.smothie1,R.drawable.omletherb1};
int index = 0;
ImageView img;
Handler hand = new Handler();

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    LinearLayout layout= (LinearLayout)findViewById(R.id.LinearView1);
    hand.postDelayed(run, GET_DATA_INTERVAL);
}

Runnable run = new Runnable() {
    public void run() {
        layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
        if (index == images.length)
            index = 0;
        hand.postDelayed(run, GET_DATA_INTERVAL);

任何帮助将不胜感激:D感谢

编辑:我得到的错误在这一行:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);

它说:

-layout 无法解析

-the方法getDrawable(int) 对于类型 Object 未定义


此错误:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);

它表示:

-layout 无法解析

-方法 getDrawable(int) 对于类型 Object 未定义

请帮助:)

Hi I'm kind of beginner to android OS programming, and I got stuck with a problem, I cant figure out how to do a dynamic background, based on timers (say each 10 seconds a background changes to a different one) I have some code but it comes up with error, here's a sample:

private static final long GET_DATA_INTERVAL = 10000;
int images[] = {R.drawable.smothie1,R.drawable.omletherb1};
int index = 0;
ImageView img;
Handler hand = new Handler();

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    LinearLayout layout= (LinearLayout)findViewById(R.id.LinearView1);
    hand.postDelayed(run, GET_DATA_INTERVAL);
}

Runnable run = new Runnable() {
    public void run() {
        layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
        if (index == images.length)
            index = 0;
        hand.postDelayed(run, GET_DATA_INTERVAL);

Any help would be greatly apprieciated :D thanks

EDIT: The errors I get are on this line:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);

It says that:

-layout cannot be resolved

-the method getDrawable(int) is undefined for the type Object


This error:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);

It says that:

-layout cannot be resolved

-the method getDrawable(int) is undefined for the type Object

Please help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有 2024-12-11 02:29:01

我终于解决了,在删除了一些错误后,我想出了这个(及其工作原理):

public class CookBookActivity extends Activity {
/** 首次创建活动时调用。 */

private static final long GET_DATA_INTERVAL = 1000;
int images[] = {R.drawable.omletherb1,R.drawable.smothie1};
int index = 0;
LinearLayout img;
Handler hand = new Handler();
private LinearLayout layout;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    layout = (LinearLayout)findViewById(R.layout.main);
    hand.postDelayed(run, GET_DATA_INTERVAL);

    Typeface tf2 = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    tv2.setTypeface(tf2);


    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setTypeface(tf);


    Button mainNext = (Button) findViewById(R.id.nextScreen1);
    mainNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent();
            i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
            startActivity(i);

        }
    });
}

Runnable run = new Runnable() {
    public void run() {
        layout.setBackgroundDrawable(getDrawable(index++));
        if (index == images.length)
            index = 0;
        hand.postDelayed(run, GET_DATA_INTERVAL);

    }
};

protected Drawable getDrawable(int i) {
    // TODO Auto-generated method stub
    return getResources().getDrawable(images[i%2]);
}

}

I have finally worked it out, after removing a few errors I have came up with this (and its working) :

public class CookBookActivity extends Activity {
/** Called when the activity is first created. */

private static final long GET_DATA_INTERVAL = 1000;
int images[] = {R.drawable.omletherb1,R.drawable.smothie1};
int index = 0;
LinearLayout img;
Handler hand = new Handler();
private LinearLayout layout;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    layout = (LinearLayout)findViewById(R.layout.main);
    hand.postDelayed(run, GET_DATA_INTERVAL);

    Typeface tf2 = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    tv2.setTypeface(tf2);


    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setTypeface(tf);


    Button mainNext = (Button) findViewById(R.id.nextScreen1);
    mainNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent();
            i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
            startActivity(i);

        }
    });
}

Runnable run = new Runnable() {
    public void run() {
        layout.setBackgroundDrawable(getDrawable(index++));
        if (index == images.length)
            index = 0;
        hand.postDelayed(run, GET_DATA_INTERVAL);

    }
};

protected Drawable getDrawable(int i) {
    // TODO Auto-generated method stub
    return getResources().getDrawable(images[i%2]);
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文