进度条

来源:互联网 发布:2016易语言源码大全 编辑:程序博客网 时间:2024/05/29 03:20

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&lt;"
            android:onClick="before"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:onClick="add"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:onClick="sub"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&gt;"
            android:onClick="next"
            />
    </LinearLayout>
    <ImageView
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:id="@+id/iv_image_image"
        android:background="#ff0000"
        />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        android:id="@+id/iv_image_new"
        />

</LinearLayout>

布局文件

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#ff0000"
        android:src="@drawable/largeimage"
        android:scaleType="center"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/largeimage"
        />
</LinearLayout>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_progressbar_num"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载"
        android:onClick="download"
        />

</LinearLayout>

java代码

public class ImageActivity extends AppCompatActivity {
    //Ctrl+Alt+F
    private ImageView iv_image_image;


    private int images[]={R.drawable.s1,
                        R.drawable.s2,
                        R.drawable.s3,
                        R.drawable.s4,
                        R.drawable.s5
    };

    int currentIndex=0;
    int currentAlpha=255;
    private File[] files;
    private Bitmap bm;
    private ImageView iv_image_new;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        iv_image_image = (ImageView) findViewById(R.id.iv_image_image);
        iv_image_new = (ImageView) findViewById(R.id.iv_image_new);






        
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();
            File file=new File(sdCardPath+"/images");
            files = file.listFiles();
        }

       iv_image_image.setImageResource(images[currentIndex]);
        bm = BitmapFactory.decodeFile(files[0].getAbsolutePath());
        iv_image_image.setImageBitmap(bm);
        iv_image_image.setImageAlpha(currentAlpha);
        iv_image_image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                float x=motionEvent.getX();
                float y=motionEvent.getY();
                Bitmap bmNew=Bitmap.createBitmap(bm,(int)x,(int)y,50,50);


                iv_image_new.setImageBitmap(bmNew);
                return true;
            }
        });
    }


    public void before(View view){
        currentIndex--;
        if(currentIndex<0){
            currentIndex=0;
            Toast.makeText(ImageActivity.this, "兄弟,没得选了", Toast.LENGTH_SHORT).show();
        }
        //iv_image_image.setImageResource(images[currentIndex]);
        bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
        iv_image_image.setImageBitmap(bm);
    }
    public void next(View view){
        currentIndex++;
        if(currentIndex>=images.length){
            currentIndex=images.length-1;
            Toast.makeText(ImageActivity.this, "兄弟,没得选了", Toast.LENGTH_SHORT).show();
        }
        //iv_image_image.setImageResource(images[currentIndex]);
        bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
        iv_image_image.setImageBitmap(bm);
    }


    public void add(View view){
        currentAlpha-=20;
        if(currentAlpha<=0){
            currentAlpha=0;
            Toast.makeText(ImageActivity.this, "看不见了", Toast.LENGTH_SHORT).show();
        }
        iv_image_image.setImageAlpha(currentAlpha);
    }
    public void sub(View view){
        currentAlpha+=20;
        if(currentAlpha>=255){
            currentAlpha=255;
            Toast.makeText(ImageActivity.this, "都给你了", Toast.LENGTH_SHORT).show();
        }
        iv_image_image.setImageAlpha(currentAlpha);
    }
}

public void download(View view){
        new MyThread().start();
    }


    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int i=msg.what;
            tv_progressbar_num.setText(i+"");
        }
    };
    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            for (int i = 0; i <=100 ; i++) {
                pb_progressbar_bar.setProgress(i);
               // tv_progressbar_num.setText(i+"");
                handler.sendEmptyMessage(i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

原创粉丝点击