如何在android中为ImageView添加触控功能?

时间:2014-10-19 19:16:26

标签: java android android-layout touch

我对android很新,并且想知道如何添加某种类型的功能,而ImageView是我按下它。我的活动java文件中的当前代码如下

          package com.example.crystalbabyheaven;

          import android.app.Activity;
          import android.util.Log;
          import android.view.*;
          import android.view.View.OnTouchListener;
          import android.os.Bundle;
          import android.os.Handler;
          import android.view.Menu;
          import android.view.MenuItem;
          import android.widget.ScrollView;
          import android.widget.ImageView; 

          public class MainActivity extends Activity implements Runnable {


              @Override
              protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);


                final ScrollView sv = (ScrollView) findViewById(R.id.ScrollView);


                Handler h = new Handler();

                h.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        sv.scrollTo(0, 8000);            
                    }
                }, 100);

                ImageView player = new ImageView(this);
                player.setImageDrawable(getResources().getDrawable(R.drawable.crystalbaby));
                player.setOnTouchListener(new OnTouchListener(){
                    @Override
                    public boolean onTouch(View v, MotionEvent event){
                        sv.scrollTo(0, 5000);
                        return true;
                    }


                }); 

              }

              @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;
              }

              /*@Override
              public void onWindowFocusChanged(boolean hasFocus) {
                  super.onWindowFocusChanged(hasFocus);

                  ScrollView sv = (ScrollView) findViewById(R.id.ScrollView);
                  sv.scrollTo(300, 300);
              }*/

              @Override
              public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                    return true;
                }
                return super.onOptionsItemSelected(item);
              }



            @Override
              public void run() {
                // TODO Auto-generated method stub

              }
          }

这是我的XML:

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/mainLayout"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
         tools:context="com.example.crystalbabyheaven.MainActivity" >



    <ScrollView
        android:id = "@+id/ScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:src="@drawable/bg" />



      </ScrollView>

    <ImageView
                android:id="@+id/crystalBaby"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_gravity="bottom"    
                android:src="@drawable/crystalbaby" />




     </FrameLayout>

我有一张非常大的图像,从地球的图片开始,向上滚动通过大气层进入太空。我在此背景上有另一个图像视图。 onCreate中我的代码的第一部分将scrollview视图设置为图像的底部。在这种情况下我想要做的是当我按下角落里的图像时,大致滚动到图像的中间。我实际上想要一些不同的东西,但我想在此之前测试一下。我的代码中有什么问题吗?我不知所措。

1 个答案:

答案 0 :(得分:0)

您正在创建一个ImageView播放器,但这与xml中的imageview不同。为什么不这样做:Imageview player =(ImageView)findViewById(R.id.imageview_player)  并在你的xml中,在imageview中放入android:id =“@ + id / imagview_player”。

或者,如果您必须以编程方式创建imageview,请将其添加到布局中。您不会在代码中的任何位置将其添加到布局中。