首页 > 技术文章 > ③安卓学习(分享案例):图片放大及透明度 界面横竖向切换

Alandre 2014-03-27 16:45 原文

前言

  最近,右眼皮还在跳,我尝试了眼药水。哈哈有点小心慌。给你们个好建议

 

       

        睡前,泡泡脚(自然边泡脚边看书啥的)。泡脚好处很多哦!我突然意识到,它能减轻压力。

        

安卓历程

    ①计划中的安卓学习

    ②迷彩灯   图片切换  跟随手指的圈

      ③图片放大及透明度  界面横竖向切换

 

  案例,我喜欢联想。最后面我会分享今天的源代码。 

图片放大及透明度 

  效果图  

    

  

   核心代码

 

      MainActivity.java

package sedion.jeffli.action;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    int [] images = new int[]{
            R.drawable.lijiang,
            R.drawable.qiao,
            R.drawable.shuangta,
            R.drawable.shui,
            R.drawable.xiangbi,
    };
    //定义默认显示图片
    int currentImg = 2;
    //定义图片初始化透明度
    private int alpha = 255;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final Button plus = (Button) findViewById(R.id.button1);
        final Button minus = (Button) findViewById(R.id.button2);
        final Button next = (Button) findViewById(R.id.button3);
        final ImageView image1 = (ImageView)findViewById(R.id.iamge1);
        final ImageView image2 = (ImageView)findViewById(R.id.iamge2);
        
        next.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                if(currentImg >= 4){
                    currentImg = -1;
                }
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
                
                //图片没回收,强制回收
                if (!bitmapDrawable.getBitmap().isRecycled()) {
                    bitmapDrawable.getBitmap().recycle();
                }
                //改变显示图片
                image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), images[++currentImg]));
            }
        });
        
        OnClickListener clickListener = new OnClickListener() {
            
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                
                if (v == plus) {
                    setAlpha(getAlpha() + 20);
                }
                if(getAlpha() >= 255){
                    setAlpha(255);
                }
                if(getAlpha() <= 0){
                    setAlpha(0);
                }
                image1.setAlpha(getAlpha());
            }
        };
        
        //保定道两个按钮
        plus.setOnClickListener(clickListener);
        minus.setOnClickListener(clickListener);
        
        image1.setOnTouchListener(new OnTouchListener(){

            @SuppressWarnings("deprecation")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
                //获取位图
                Bitmap bitmap = bitmapDrawable.getBitmap();
                //bitmap实际大小与第一个ImageView的缩放比例
                double scale = bitmap.getWidth() / 320.0;
                //获取需要显示的图片的开始点
                int x = (int) (event.getX() * scale);
                int y = (int) (event.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(getAlpha());
                return false;
            }
            
        });
        
        
        
        
        
        
        
        
        
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public int getAlpha() {
        return alpha;
    }

    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

}
View Code

      activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:gravity="center">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=" next" />
    </LinearLayout>
      <!--  显示图片整体-->
      <ImageView 
          android:id="@+id/iamge1"
          android:layout_width="fill_parent"
          android:background="#0000ff"
          android:layout_height="240px"
          android:src="@drawable/shuangta"
          android:scaleType="fitCenter"
          />
      <!--  定义显示图片局部细节-->

      <ImageView 
          android:id="@+id/iamge2"
          android:layout_width="120dp"
          android:background="#0000ff"
          android:layout_height="120dp"
          android:layout_marginTop="10dp"
          />
</LinearLayout>
View Code

 

   联想

      用于一些特殊科学图解。或者医学院那些解剖图,血管细胞啥啥的之类,可以学习必备哦(原谅本人在医科大)

  

界面横竖向切换

  效果图

          

               

      

      

    核心代码

 

      MainActivity.java

package sedion.jeffli.action;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
        final LinearLayout test = (LinearLayout)findViewById(R.id.test);
        toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(isChecked){
                    //垂直
                    test.setOrientation(1);
                }else{
                    //水平
                    test.setOrientation(0);
                }
            }
            
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
View Code

      activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ToggleButton 
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="水平分布"
        android:textOn="垂直分布"
        android:checked="true"
        />
    <LinearLayout 
        android:id="@+id/test"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="jeff"/>
        <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="li"/>
        <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="jeffli"/>
    </LinearLayout>
</LinearLayout>
View Code

 

    联想

      这东西,以后用户喜欢哪种随意切换多么好。以后安卓用户体验可以考虑.

 

补言

           

        数学美吧!

            

        最近在看这本书,吴军大哥写的很好看。google我的目标!啦啦啦

感谢及资源共享

    感谢读者!很喜欢你们给我的支持。如果支持,点个赞。

    知识来源: http://www.oschina.net/android/books

    上面案例资源分享:

      链接:链接:http://pan.baidu.com/share/link?shareid=258699827&uk=3307409781 密码:e9pk

    

推荐阅读