Android的ImageView介绍-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>
效果图
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。