An example of a picture viewer that can be adjusted transparently by Android _android

Source: Internet
Author: User
Tags stub

This article illustrates an example of an Android based image viewer implementation that can be used to adjust transparency, as follows:

main.xml Part of the code is as follows:

<?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 " > <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" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "reduce transparency"/> <button android:id= "@+id/button" 3 "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:text=" Next "/> & Lt;/linearlayout> <!--define ImageView--> <imageview android:id= "@+id/imageview1" to display the overall picture Android:lay Out_width= "Wrap_content" android:layout_height= "Wrap_content" android:background= "#0000ff" android:scaletype= "Fitcenter" android:src= "@drawable Shuangta "/> <!--defines imageview--> <imageview android:id=" @+id/imageview2 "that display local images Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "10DP" android:background= "#

 0000ff "/> </LinearLayout>

The

Java part of the code is:

Package Android.demo;
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 Androiddemo5activity extends activity {//define an array to access the picture int[] images = new int[] {R.drawable.lijiang, R.D
  Rawable.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.oncreate
    (savedinstancestate);
    Setcontentview (R.layout.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 Nextbutton.setonclicklistener to view the next picture (new Onclicklistener () {@Override public void OnClick (Vi
        EW v) {if (Currentimage >= 5) {currentimage =-1;
        } bitmapdrawable bitmap = (bitmapdrawable) imageview1. getdrawable ();
        If the picture has not been reclaimed, force the recall of the picture if (!bitmap.getbitmap (). isrecycled ()) {Bitmap.getbitmap (). Recycle (); ///change ImageView picture Imageview1.setimagebitmap (Bitmapfactory.decoderesource (getresources), IM
      Ages[++currentimage]));

    }
    }); Define a way to change the transparency of a picture onclicklistener listener = new Onclicklistener () {@Override public void OnClick (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 picture Imageview1.setalpha (Alpha);

    }
    };
    Add Listener Plusbutton.setonclicklistener (Listener) for 2 buttons;
    Minuxbutton.setonclicklistener (listener);  Imageview1.setontouchlistener (New Ontouchlistener () {@Override public boolean Ontouch (View arg0, motionevent
            ARG1) {//TODO auto-generated method stub bitmapdrawable Bitmapdeaw = (bitmapdrawable) 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 start point int x = (int) (ARG1.GETX () * scale) of the picture;
        int y = (int) (arg1.gety () * scale);
        if (x + > Bitmap.getwidth ()) {x = Bitmap.getwidth ()-120; }
        if (Y + > Bitmap.getheight ()) {y = Bitmap.getheight ()-120;
        ///Display the specified area of the picture Imageview2.setimagebitmap (Bitmap.createbitmap (Bitmap, x, y, 120, 120));
        Imageview2.setalpha (Alpha);
      return false;
  }
    });

 }

}

The Operation effect chart is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.