In it can only respond to onLongClick () listener event ? Great God who help me !
package com.qualitypicture.activity;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.qualitypicture.R;
import com.qualitypicture.activity.more.FeedbackAcitivity;
import com.qualitypicture.util.FileUtil;
public class TabMyPictureActivity extends Activity
{
public Bitmap bit;
private ImageView imageview;
private TextView textview;
int image_alpha = 255;
int i = 0;
private FileUtil file01 = new FileUtil();
private int length = file01.GetPictureList().length;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_my_picture);
imageview = (ImageView) this.findViewById(R.id.ImageView01);
imageview.setOnLongClickListener(new ImageOnLongClickListener());
// 开启一个线程
new Thread(new Runnable()
{
public void run()
{
}
}).start();
bit = null;
bit = file01.getlmageAt(file01.fileNames, i);
// ---调整图片显示大小
int width = bit.getWidth();// 获取真实宽高
int height = bit.getHeight();
View view = new View(TabMyPictureActivity.this);
LayoutParams lp = view.getLayoutParams();
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int layoutHeight = (height * screenWidth) / width;// 调整高度
if (lp == null)
{
lp = new LayoutParams(screenWidth, layoutHeight);
}
view.setLayoutParams(lp);
// ------结束
imageview.setImageBitmap(bit);
imageview.invalidate();
imageview.setAlpha(image_alpha);
}
long firstTime;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
long secondTime = System.currentTimeMillis();
if (secondTime - firstTime > 2000)
{// 如果两次按键时间间隔大于2000毫秒,则不退出
Toast.makeText(TabMyPictureActivity.this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
firstTime = secondTime;// 更新firstTime
return true;
} else
{
System.exit(0);// 否则退出程序
}
return false;
} else if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0)
{
return true; // 返回true就不会弹出默认的setting菜单
}
return false;
}
/**
* 当用户每次触摸时,会产生1个MotionEvent事件 其中包含1个 ACTION_DOWN, 多个ACTION_MOVE,
* 1个ACTION_UP触发
*/
float xDown = 0, yDown = 0, xUp = 0, yUp = 0; // 分别是:在按下、离开屏幕的X、Y坐标
float addx = 0, addy = 0; // 连续2个ACTION_MOVE之间的X、Y坐标的增量。
@Override
public boolean onTouchEvent(MotionEvent me)
{
if (me.getAction() == MotionEvent.ACTION_DOWN)
{
xDown = me.getX();
System.out.println("11111111");
yDown = me.getY();
} else if (me.getAction() == MotionEvent.ACTION_UP)
{
xUp = me.getX();
yUp = me.getY();
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
if (Math.abs(xUp - xDown) >= width / 4 && Math.abs(yUp - yDown) <= Math.abs(xUp - xDown))
{
// 切换到下一个
if (xUp > xDown)
{
Bitmap bit = null;
if (i < length)
{
i++;
if (i == length)
i = 0;
bit = file01.getlmageAt(file01.fileNames, i);
}
System.out.println("2222222222");
imageview.setImageBitmap(bit);
imageview.invalidate();
imageview.setAlpha(image_alpha);
} else if (xUp < xDown)
{
Bitmap bit = null;
if (i < length)
{
i--;
if (i < 0)
i = length - 1;
bit = file01.getlmageAt(file01.fileNames, i);
}
System.out.println("3333333333333");
imageview.setImageBitmap(bit);
imageview.invalidate();
imageview.setAlpha(image_alpha);
}
}
}
return true;
}
class ImageOnLongClickListener implements OnLongClickListener
{
@Override
public boolean onLongClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(TabMyPictureActivity.this);
builder.setTitle("列表框");
builder.setItems(new String[]
{ "设置壁纸", "删除图片" }, new ItemOnclickListener());
builder.setNegativeButton("确定", null);
builder.show();
return false;
}
}
class ItemOnclickListener implements android.content.DialogInterface.OnClickListener
{
@Override
public void onClick(DialogInterface dialog, int which)
{
System.out.println("which " + which);
if (which == 0)
{
try
{
TabMyPictureActivity.this.setWallpaper(bit);
Toast.makeText(TabMyPictureActivity.this, "设置成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
------ Solution ------------------------------------- -------
onTouchEvent Why write dead return value true, changed back super.onTouch try
------ Solution --------------- -----------------------------
http://www.cnblogs.com/Tiger-Dog/articles/1944791. html
suggested that the coexistence of a variety of events , all in ontouch in treatment, but also relatively simple .
------ Solution ---------------------------------------- ----
onTouchEvent of return true; masked subsequent event dispatching it
------ For reference only ----------------- ----------------------
If you think this is easy also possible.
Estimate your problem lies in the imageview on size of this view may occupy a significant position , you trigger the first event in the imageview , then ontouch will not pass down . xml where you look , if only part of the region , then in the other non- imageview area , ontouch event is certainly intercepted .
没有评论:
发表评论