标签:android
imageView继承于View,主要用于显示图片,凡是Drawable对象都可以用它显示。
ImageView直接派生了ImageButton和ZoomButton等组件。
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学习之旅(二十二)
标签:android
原文地址:http://blog.csdn.net/lpjishu/article/details/46042049