본문 바로가기

Android/Long Tab & ImageView

Android Custom Long Tab & Dynamic ImageView

Long Tab 테스트
ImageView에 동적으로 이미지를 설정하여 스크린을 2초이상 터치하면 이미지가 팝업되는 예제

CountDownTimer, ImageView 등을 사용하여 테스트했다.
스크린의 터치된 위치와 이미지가 나타나는 위치의 Y값이 차이를 보이는데, 이 문제를 해결하지는 못했다.
화면 상단의 상태바와 타이틀바의 높이를 고려해야 할 것 같다.

<?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">
   
 <AbsoluteLayout
  android:id="@+id/absLayout"
  android:layout_width="fill_parent"
  android:layout_heightfill_parent">

  <TextView
   android:id="@+id/textView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_x="0px"
      android:layout_y="0px"
      android:text="2초이상 스크린을 터치하면 그 위치에 이미지가 출력되는 예제, 이미지를 다시 터치하면 이미지가 사라진다"/>

     <ImageView
      android:id="@+id/sampleImg"
      android:layout_width="200px"
      android:layout_height="80px"
      android:src="@drawable/icon"
      android:layout_x="0px"
      android:layout_y="0px"
      android:visibility="gone">
     </ImageView>
    
    </AbsoluteLayout>
</LinearLayout>


 

package com.dearpeople.android.test.longtab;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class LongTabActivity extends Activity {
   
  boolean longtab;
  TextView textView;
  ImageView imageView;
  float x,y;
  int titleBarHeight;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AbsoluteLayout absLayout = (AbsoluteLayout)this.findViewById(R.id.absLayout);
        textView = (TextView)this.findViewById(R.id.textView);

        imageView = (ImageView)this.findViewById(R.id.sampleImg);
        imageView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    imageView.setVisibility(View.GONE);
   }
  });
  // 필요하다면 다음과 같은 방법으로 ImageView를 동적으로 생성할 수도 있다.
  //ImageView imgView = new ImageView(this);
  //imgView.setImageResource(R.drawable.longtap_bg);
 }

private void showPopupView(android.os.Message msg) {
          if(!longtab) return;
          imageView.setImageResource(R.drawable.longtap_bg);

          // 마우스가 눌린 지점의 Y좌표에서 105를 빼 주어야 그 위치에 이미지가 출력된다.
          // 타이틀바와 상태바의 높이를 고려해 주어야 할 것 같다.
          AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(100, 100, (int)x, (int)y-105);
          imageView.setLayoutParams(params);
          imageView.setVisibility(View.VISIBLE);
 }

 @Override
  public boolean onTouchEvent(MotionEvent event) {
   int action = event.getAction();
  
   TabCount timer = new TabCount(2000, 2000);
  
   if(action==MotionEvent.ACTION_MOVE){
   
   }else if(action==MotionEvent.ACTION_DOWN){
    x = event.getX();
    y = event.getY();
    timer.start();
    longtab = true;
  }else if(action==MotionEvent.ACTION_UP){
   longtab = false;
  }
  return super.onTouchEvent(event);
  }
    
  /*내부클래스로 선언한 타이머 */
  class TabCount extends CountDownTimer {
 
   public TabCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
   }
 
   @Override
   public void onFinish() {
      showPopupView();
   }
 
   @Override
   public void onTick(long arg0) {
   }
 
  }// 내부클래스 끝
}