Android的ImageView介紹-android學習之旅(二十二)

來源:互聯網
上載者:User

標籤:android

ImageView簡介

imageView繼承於View,主要用於顯示圖片,凡是Drawable對象都可以用它顯示。
ImageView直接派生了ImageButton和ZoomButton等組件。

ImageView的屬性設定


android:sacleType的設定是用來設定圖片的顯示方式。

圖片瀏覽器執行個體
package peng.liu.testview;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.os.SystemClock;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.Chronometer;import android.widget.ImageView;public class MainActivity extends Activity implements View.OnClickListener{    private int[] images = new int[]{            R.drawable.ic_launcher,            R.drawable.ic_launcher,            R.drawable.ic_launcher,            R.drawable.ic_launcher,            R.drawable.ic_launcher,    };    private int currentImg = 2;    private int alpha = 0;    private Button plus,minus,next;    private ImageView image1,image2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        plus = (Button) findViewById(R.id.plus);        minus = (Button) findViewById(R.id.minus);        next = (Button) findViewById(R.id.next);        image1 = (ImageView) findViewById(R.id.image1);        image2 = (ImageView) findViewById(R.id.image2);        next.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                image1.setImageResource(images[++currentImg%images.length]);            }        });        plus.setOnClickListener(this);        minus.setOnClickListener(this);        image1.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View view, MotionEvent motionEvent) {                BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();                Bitmap bitmap = bitmapDrawable.getBitmap();                double scale = bitmap.getWidth()/320.0;                int x = (int) (motionEvent.getX()*scale);                int y = (int) (motionEvent.getY()*scale);                if (x+120>bitmap.getWidth()){                    x = bitmap.getWidth()-120;                }                if (y+120>bitmap.getHeight()){                    y = bitmap.getHeight()-120;                }                image2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120));                image2.setAlpha(alpha);                return false;            }        });    }    @Override    public void onClick(View view) {        if (view == plus){            alpha+=20;        }        if (view == minus){            alpha-=20;        }        if (alpha>=255){            alpha = 255;        }        if (alpha <= 0){            alpha = 0;        }    }}
布局代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity"    android:orientation="vertical"> <LinearLayout     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="horizontal">     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/plus"         android:text="plus"/>     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/minus"         android:text="minus"/>     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/next"         android:text="next"/>     </LinearLayout>    <ImageView        android:layout_width="fill_parent"        android:layout_height="240dp"        android:src="@drawable/hello"        android:id="@+id/image1"/>    <ImageView        android:layout_width="120dp"        android:layout_height="120dp"        android:src="#f00"        android:id="@+id/image2"        android:layout_gravity="center_horizontal"/></LinearLayout>

Android的ImageView介紹-android學習之旅(二十二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.