The picture browser calls ImageView's Setalpha method to achieve a change in image transparency.
The Main.xml file is as follows: Three buttons, two ImageView, interface definition Two ImageView, one is to display the local picture imageview,android:scaletype= "Fitcenter indicates that the picture will keep the horizontal ratio zoom, and place the zoomed picture in the center of the ImageView.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G T <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > <Bu Tton android:id= "@+id/button1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "increase transparency"/> <button android:id= "@+id/button2" and Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "reduce transparency"/> <button android:id= "@+id/button3" android:layout_width= "Wrap_content" Android:layo ut_height= "Wrap_content" android:text= "Next"/> </LinearLayout> <!--definition shows the overall picture of ImageView-- <imageview AndroidOid:id= "@+id/imageview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and Roid:background= "#0000ff" android:scaletype= "Fitcenter" android:src= "@drawable/shuangta"/> <!--definition Show ImageView of local pictures---<imageview android:id= "@+id/imageview2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:background= "#0000ff"/> </LinearLayout>
Set Listener, button to change the image's alpha value, add Ontouchlistener on the first ImageView, when the touch event occurs, the program extracts the corresponding part of the picture from the original, and displays it in the second ImageView.
Package Com.example.imageview;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.drawable.bitmapdrawable;import Android.os.Bundle;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.ontouchlistener;import Android.widget.button;import Android.widget.imageview;public Class Mainactivity extends Activity {//define an array of access images int[] images = new int[] {R.drawable.lijiang, R.drawable.qiao, R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi, R.drawable.ic_launcher,}; Defines the currently displayed picture int currentimage = 2; Defines the initial transparency of a picture private int alpha = 255; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.o Ncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Final Button Plusbutton = (Button) Findviewbyid (R.id.button1); Final Button Minuxbutton = (Button) Findviewbyid (R.id.button2); Final Button Nextbutton = (Button) Findviewbyid (R.id.button3); Final ImageView Imageview1 = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Final ImageView imageview2 = (ImageView) Findviewbyid (R.ID.IMAGEVIEW2); Defines the time listener for viewing the next picture Nextbutton.setonclicklistener (new Onclicklistener () {@Override public voi D OnClick (View v) {if (Currentimage >= 5) {currentimage =-1; } bitmapdrawable bitmap = (bitmapdrawable) imageview1. getdrawable (); If the picture has not yet been recycled, first force the Recycle picture if (!bitmap.getbitmap (). isRecycled ()) {Bitmap.getbitmap (). Recycl E (); }//Change the picture of ImageView Imageview1.setimagebitmap (Bitmapfactory.decoderesource ( Getresources (), images[++currentimage]); } }); Defines how to change the transparency of a picture onclicklistener listener = new Onclicklistener () {@Override public void O Nclick (View v) {if (v = = Plusbutton) {alpha + = 20; } if (v = = Minuxbutton) {alpha-= 20; } if (Alpha > 255) {alpha = 255; } if (Alpha <= 0) {alpha = 0; }//Change the transparency of the image Imageview1.setalpha (alpha); } }; Add Listener Plusbutton.setonclicklistener (Listener) for 2 buttons; Minuxbutton.setonclicklistener (listener); Imageview1.setontouchlistener (New Ontouchlistener () {@Override public boolean OnTouch (View arg0, Mo Tionevent arg1) {//TODO auto-generated method stub bitmapdrawable Bitmapdeaw = (bitmapdraw Able) imageview1. getdrawable (); Gets the bitmap in the first picture display box Bitmap Bitmap = Bitmapdeaw.getbitmap (); Double scale = bitmap.getwidth (); You may need to display the starting point of the picture int x = (int) (ARG1.GETX () * scale); int y = (int) (arg1.gety () * scale); if (x + > Bitmap.getwidth ()) {x = Bitmap.getwidth ()-120; } if (Y + > Bitmap.getheight ()) {y = Bitmap.getheight ()-120; }//Displays the specified area of the picture Imageview2.setimagebitmap (Bitmap.createbitmap (Bitmap, x, Y, 120, 120)); Imageview2.setalpha (Alpha); return false; } }); }}
Android footstep---Simple picture browser changes image transparency