Android實現非本地圖片的點擊效果

來源:互聯網
上載者:User

mainActivity如下:

package cn.c;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;/** * 需求描述: * ImageView載入網狀圖片,在點擊ImageView的時候實現點擊的效果 * 類似於本地圖片的相關操作 * 實現方式: * 對於ImageView實現OnTouchListener()處理其點擊事件 * 在點擊事件時改變ImageView控制項的Alpha * 注意問題: * onTouch()中return true;否則只能監聽到按下 * 不能監聽到抬起 *  */public class MainActivity extends Activity {   private ImageView mImageView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        init();    }    private void init(){    mImageView=(ImageView) findViewById(R.id.imageView);    //擷取網狀圖片的過程,省略    //mImageView.setImageBitmap(bitmap from network);    mImageView.setOnTouchListener(new TouchListenerImpl());    }    private class TouchListenerImpl implements OnTouchListener{public boolean onTouch(View v, MotionEvent event) {ImageView imageView=(ImageView) v;//按下if (event.getAction()==MotionEvent.ACTION_DOWN) {System.out.println("down down down ");imageView.setAlpha(0);imageView.invalidate();}//抬起if (event.getAction()==MotionEvent.ACTION_UP||   event.getAction()==MotionEvent.ACTION_CANCEL) {System.out.println("up up up ");imageView.setAlpha(200);imageView.invalidate();}return true;}    } }

main.xml如下:

<RelativeLayout 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" >    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="ImageView載入的是網狀圖片,在點擊ImageView的時候實現點擊的效果.類似於本地圖片的相關操作"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"    />       <ImageView        android:id="@+id/imageView"        android:layout_width="80dip"        android:layout_height="80dip"        android:src="@drawable/ic_launcher"        android:layout_centerInParent="true"     /></RelativeLayout>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.