(一)
知识点:
(1)imageView.setImageAlpha(Alpha):设置图片透明度
(2)在布局imageView中设置图片位置:android:scaleType="center"
android:scaleType="center"
1.效果图:点击增加图片透明度增加,点击减少图片透明度减少,点击下一张显示下一张图片
2.activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.administrator.hello3.MainActivity" 11 android:orientation="vertical" 12 android:layout_gravity="center" 13 > 14 15 <LinearLayout 16 android:orientation="horizontal" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_gravity="center"> 20 <Button 21 android:id="@+id/btn_add" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="增加" /> 25 <Button 26 android:id="@+id/btn_reduce" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="减少" /> 30 <Button 31 android:id="@+id/btn_next" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="下一张" /> 35 </LinearLayout> 36 <ImageView 37 android:layout_gravity="center" 38 android:id="@+id/iv" 39 android:src="@mipmap/ic_launcher" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" /> 42 </LinearLayout>
3.MainActivity.java
1 package com.example.administrator.hello3; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.ImageView; 8 9 public class MainActivity extends AppCompatActivity { 10 private Button button_add,button_reduce,button_next; 11 private ImageView imageView; 12 private int alpha=255; 13 private int current=0; 14 private int[] pics = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4}; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 setContentView(R.layout.activity_main); 22 23 button_add=(Button)findViewById(R.id.btn_add); 24 button_reduce=(Button)findViewById(R.id.btn_reduce); 25 button_next=(Button)findViewById(R.id.btn_next); 26 imageView=(ImageView) findViewById(R.id.iv); 27 28 MyListener myListener = new MyListener(); 29 button_add.setOnClickListener(myListener); 30 button_reduce.setOnClickListener(myListener); 31 32 button_next.setOnClickListener(new View.OnClickListener() { 33 @Override 34 public void onClick(View v) { 35 imageView.setImageResource(pics[current++%pics.length]); 36 } 37 }); 38 39 40 41 } 42 class MyListener implements View.OnClickListener{ 43 @Override 44 public void onClick(View v) { 45 if(v==button_add){ 46 alpha+=20; 47 if(alpha>=255){ 48 alpha=255; 49 } 50 }else if(v==button_reduce) { 51 alpha -= 20; 52 if (alpha <= 0) { 53 alpha = 0; 54 } 55 } 56 imageView.setImageAlpha(alpha); 57 } 58 } 59 60 }
3.drawable下面加入自己的图片
原文地址:https://www.cnblogs.com/sunxiaoyan/p/9054764.html
时间: 2024-11-06 09:54:40