Android --- 21 --- ImageView, androidimageview

Source: Internet
Author: User

Android --- 21 --- ImageView, androidimageview


MainActivity. java:


Import android. app. activity; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. OS. bundle; import android. view. menu; import android. view. menuItem; 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; import android. widget. linearLayout; public class MainActivity extends Activity {// defines an array of image access int images [] = new int [] {R. drawable. yy00, R. drawable. yy01, R. drawable. yy02, R. drawable. yy03, R. drawable. yy04, R. drawable. yy05, R. drawable. yy06, R. drawable. yy07, R. drawable. yy08, R. drawable. yy09, R. drawable. yy11, R. drawable. yy12, R. drawable. yy13 ,}; // define the current image int currentimage = 2; private int alpha = 255; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final Button plus = (Button) findViewById (R. id. plus); final Button minus = (Button) findViewById (R. id. minus); final ImageView imageView = (ImageView) findViewById (R. id. image1); final ImageView imageView2 = (ImageView) findViewById (R. id. image2); Button next = (Button) findViewById (R. id. next); next. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubimageView. setImageResource (images [++ currentimage % images. length]) ;}}); OnClickListener listener = new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubif (v = plus) {alpha + = 20;} if (v = minus) {alpha-= 20;} if (alpha> = 255) {alpha = 255 ;} if (alpha <= 0) {alpha = 0;} imageView. setAlpha (alpha) ;}}; // change transparency plus. setOnClickListener (listener); minus. setOnClickListener (listener); imageView. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubBitmapDrawable bitmapDrawable = (BitmapDrawable) imageView. getDrawable (); // obtain the Bitmap bitmap = bitmapDrawable In the first image display box. getBitmap (); // double scale = bitmap. getWidth ()/320.0; // get the start point int x = (int) (event. getX () * scale); int y = (int) (event. getY () * scale); if (x + 120> bitmap. getWidth () {x = bitmap. getWidth ()-120;} if (y + 120> bitmap. getHeight () {y = bitmap. getHeight ()-120;} // display the image in the specified region imageView2.setImageBitmap (Bitmap. createBitmap (bitmap, x, y, 120,120); imageView2.setAlpha (alpha); return false ;}});}}


Activity_main.xml:


<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/activity_horizontal_margin" android: paddingRight = "@ dimen/activity_horizontal_margin" android: DdingTop = "@ dimen/activity_vertical_margin" tools: context = "com. example. imageviewdemo. mainActivity "> <LinearLayout android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: gravity =" center "android: orientation =" horizontal "> <Button android: id = "@ + id/plus" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Increase transparency"/> <Button android: id = "@ + id/m Inus "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" Transparency reduction "/> <Button android: id = "@ + id/next" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "next"/> </LinearLayout> <! -- Define the overall ImageView of the displayed image --> <ImageView android: id = "@ + id/image1" android: layout_width = "fill_parent" android: layout_height = "240px" android: scaleType = "fitCenter" android: src = "@ drawable/yy01"/> <! -- Define the imageView for displaying partial images --> <ImageView android: id = "@ + id/image2" android: layout_width = "120dp" android: layout_height = "120dp" android: layout_marginTop = "10dp" android: background = "# 00f"/> </LinearLayout>



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.