android imgeview 暂停和清除

发布于 2024-11-01 09:36:33 字数 1824 浏览 0 评论 0原文

我有这段代码,我正在尝试从 SD 卡读取 2 张图像,但我不知道如何在这 2 张图像之间暂停,以便我有足够的时间查看每张图像。另外,我不知道如何在显示 2 个图像后清除 ImgeView

这就是我想要的方式:

load first image -> wait for 5 sec -> load sec image-> wait for 5 sec clear the ImageView

MainActivity.java

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class ReadfromSD extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String imageInSD = "/sdcard/Hanud/AD2.jpg";
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView) findViewById(R.id.imageview1);
        myImageView.setImageBitmap(bitmap);

        String imageInSD2 = "/sdcard/Hanud/AD1.jpg";
        Bitmap bitmap2 = BitmapFactory.decodeFile(imageInSD2);
        ImageView myImageView1 = (ImageView) findViewById(R.id.imageview1);
        myImageView1.setImageBitmap(bitmap2);
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:scaleType="center" />
    
</LinearLayout>

I have this code and I'm trying to read 2 images from the SD card and I don't know how to pause between the 2 images that I can have enough time to see each 1 of them. Also, I don't know how to clear the ImgeView after I display the 2 images

This is how I want it to go:

load first image -> wait for 5 sec -> load sec image-> wait for 5 sec clear the ImageView

MainActivity.java

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class ReadfromSD extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String imageInSD = "/sdcard/Hanud/AD2.jpg";
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView) findViewById(R.id.imageview1);
        myImageView.setImageBitmap(bitmap);

        String imageInSD2 = "/sdcard/Hanud/AD1.jpg";
        Bitmap bitmap2 = BitmapFactory.decodeFile(imageInSD2);
        ImageView myImageView1 = (ImageView) findViewById(R.id.imageview1);
        myImageView1.setImageBitmap(bitmap2);
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:scaleType="center" />
    
</LinearLayout>

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

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

发布评论

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

评论(1

ぽ尐不点ル 2024-11-08 09:36:33
public class ImgLoadAct extends Activity {
/** Called when the activity is first created. */
Timer timer;
ImageView myImageView;
TextView titleText;
Drawable img1Drawable;
Drawable img2Drawable;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myImageView = (ImageView)findViewById(R.id.imageview1);

    titleText=(TextView)findViewById(R.id.img_title);

    img1Drawable=getResources().getDrawable(R.drawable.android_logo);
    img2Drawable=getResources().getDrawable(R.drawable.apple_logo);
    //Load Img1 android_logo

    myImageView.setImageDrawable(img1Drawable);
    titleText.setText("Img2 android_logo.png loaded");

    timer=new Timer();
    //delay  amount of time(5s here) in milliseconds before first execution.
    timer.schedule(loadImg2, 5000);


}
TimerTask loadImg2 = new TimerTask(){

    @Override
    //Load Img2
    public void run() {
         runOnUiThread(new Runnable(){    
             public void run() {    
                 titleText.setText("Img2 apple_logo.png loaded");
                 myImageView.setImageDrawable(img2Drawable);
                 timer.cancel();
                 timer=new Timer();
                 timer.schedule(clearImg, 5000);
                 }  
         });

    } 

};
TimerTask clearImg = new TimerTask(){

    @Override
    public void run() {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable(){    
            public void run() {    
             timer.cancel();
             titleText.setText("Img cleared");
             myImageView.setImageDrawable(null);

                }  
        });
    }    


   } ;

代码下载:
http://www.everbox.com/f/UtxQ4gzfXBPTFZlcIXi3HQHGU4

public class ImgLoadAct extends Activity {
/** Called when the activity is first created. */
Timer timer;
ImageView myImageView;
TextView titleText;
Drawable img1Drawable;
Drawable img2Drawable;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myImageView = (ImageView)findViewById(R.id.imageview1);

    titleText=(TextView)findViewById(R.id.img_title);

    img1Drawable=getResources().getDrawable(R.drawable.android_logo);
    img2Drawable=getResources().getDrawable(R.drawable.apple_logo);
    //Load Img1 android_logo

    myImageView.setImageDrawable(img1Drawable);
    titleText.setText("Img2 android_logo.png loaded");

    timer=new Timer();
    //delay  amount of time(5s here) in milliseconds before first execution.
    timer.schedule(loadImg2, 5000);


}
TimerTask loadImg2 = new TimerTask(){

    @Override
    //Load Img2
    public void run() {
         runOnUiThread(new Runnable(){    
             public void run() {    
                 titleText.setText("Img2 apple_logo.png loaded");
                 myImageView.setImageDrawable(img2Drawable);
                 timer.cancel();
                 timer=new Timer();
                 timer.schedule(clearImg, 5000);
                 }  
         });

    } 

};
TimerTask clearImg = new TimerTask(){

    @Override
    public void run() {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable(){    
            public void run() {    
             timer.cancel();
             titleText.setText("Img cleared");
             myImageView.setImageDrawable(null);

                }  
        });
    }    


   } ;

}

Src dowload:
http://www.everbox.com/f/UtxQ4gzfXBPTFZlcIXi3HQHGU4

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