如何在Android的ImageView中添加触摸功能? [英] How can I add touch functionality to an ImageView in android?

查看:85
本文介绍了如何在Android的ImageView中添加触摸功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android很陌生,想知道如何向其中添加某种类型的功能,然后按ImageView.我的活动Java文件中的当前代码如下

I'm pretty new to android and was wondering how I could add some type of functionality to and ImageView is I press it. My current code in my activity java file is as follows

          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视图设置为图像的底部.在这种情况下,我想做的是,当我在角落按图像时,大约滚动到图像的中间.我实际上想要一些不同的东西,但是我想先进行测试.我的代码有问题吗?我很茫然.

I have a very large image that starts from a picture of the earth and scrolls upward through the atmosphere into space. I have another image view on top of this background. The first portion of my code in onCreate sets the scrollview view to the bottom of the image. What I want to do in this case is scroll approximately to the middle of the image when I press the image in the corner. I actually want something different, but I want to test this before anything. Is there something wrong in my code? I'm at a loss.

推荐答案

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

You're creating an ImageView player, but this is not the same as the imageview in your xml. Why not do: Imageview player = (ImageView)findViewById(R.id.imageview_player) and in your xml, put android:id="@+id/imagview_player" inside the imageview.

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

Or if you must create the imageview programatically, then add it to your layout. You are not adding it to your layout anywhere in the code.

这篇关于如何在Android的ImageView中添加触摸功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆