(一)

知识点:

(1)imageView.setImageAlpha(Alpha):设置图片透明度

(2)在布局imageView中设置图片位置:android:scaleType="center"

android:scaleType="center"

1.效果图:点击增加图片透明度增加,点击减少图片透明度减少,点击下一张显示下一张图片

图像视图-ImageView-LMLPHP

2.activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.hello3.MainActivity"
android:orientation="vertical"
android:layout_gravity="center"
> <LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" />
<Button
android:id="@+id/btn_reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张" />
</LinearLayout>
<ImageView
android:layout_gravity="center"
android:id="@+id/iv"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

3.MainActivity.java

 package com.example.administrator.hello3;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity {
private Button button_add,button_reduce,button_next;
private ImageView imageView;
private int alpha=255;
private int current=0;
private int[] pics = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main); button_add=(Button)findViewById(R.id.btn_add);
button_reduce=(Button)findViewById(R.id.btn_reduce);
button_next=(Button)findViewById(R.id.btn_next);
imageView=(ImageView) findViewById(R.id.iv); MyListener myListener = new MyListener();
button_add.setOnClickListener(myListener);
button_reduce.setOnClickListener(myListener); button_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(pics[current++%pics.length]);
}
}); }
class MyListener implements View.OnClickListener{
@Override
public void onClick(View v) {
if(v==button_add){
alpha+=20;
if(alpha>=255){
alpha=255;
}
}else if(v==button_reduce) {
alpha -= 20;
if (alpha <= 0) {
alpha = 0;
}
}
imageView.setImageAlpha(alpha);
}
} }

3.drawable下面加入自己的图片

图像视图-ImageView-LMLPHP

05-23 16:15