所以结果比我想象的要困难一些,但我现在几乎可以正常工作了。
这是我最终使用的 OnTouchListener:
listOnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me){
if (me.getAction() == MotionEvent.ACTION_DOWN){
//This means a finger has come down on top of our view
//We are going to start the animation now.
Log.i(myTag, "Action Down");
Context mContext = getApplicationContext();
Resources res = mContext.getResources();
TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.listtransition);
v.setBackgroundDrawable(transition);
//LongClick took approx. 510-530 milliseconds to register after OnTouch. So I set the duration at 500 millis.
transition.startTransition(500);
}else if (me.getAction() == MotionEvent.ACTION_UP){
//This means we didn't hold long enough to get a LongClick
//Set the background back to the normal one.
v.setBackgroundResource(R.drawable.bubblelight);
}else if (me.getAction() == MotionEvent.ACTION_MOVE){
//Do Nothing
}else if (me.getAction() == MotionEvent.ACTION_CANCEL){
Log.i(myTag, "Action Cancel");
//This means we are scrolling on the list, not trying to longpress
//So set the background back to the normal one.
v.setBackgroundResource(R.drawable.bubblelight);
}
return false;
}
};
我还在其中使用了一个 OnLongClickListener 我将背景设置回正常的。
这是过渡 XML:
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bubblelight1" />
<item android:drawable="@drawable/bubbledark" />
</transition>
您可能会问,bubblelight1 是怎么回事?稍后会详细介绍。
这是我在适配器中使用的 getView() 方法,用于返回列表中显示的视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Status s = items.get(position);
if (s != null) {
v.setOnTouchListener(listOnTouchListener);
v.setOnLongClickListener(listOnLongClickListener);
tweetTxt = (TextView) v.findViewById(R.id.tweetTxt);
timeTxt = (TextView) v.findViewById(R.id.timeTxt);
if (tweetTxt != null) {
tweetTxt.setText(s.getText());
tweetTxt.setOnTouchListener(gestureListener);
}
if(timeTxt != null){
timeTxt.setText(getTimeFromNow(s.getCreatedAt().getTime()));
//timeTxt.setText(s.getCreatedAt().toLocaleString());
}
}
LinkifyWithTwitter.addLinks(tweetTxt, Linkify.ALL);
return v;
}
v.setOnTouchListener(listOnTouchListener);和
v.setOnLongClickListener(listOnLongClickListener);是我在上面显示的使用侦听器设置视图的行。
现在关于泡泡灯1。 bubblelight 和 bubbledark 是我第一次尝试时使用的九个补丁图像,每当过渡开始而不是背景从bubblelight 过渡到bubbledark 时,bubblelight 会变大并且bubbledark 会出现在bubblelight 内部。所以我有一个亮的大气泡,一个深色的小气泡,然后是里面的文字。为了解决这个问题,我制作了一个bubblelight 的副本,并完全填充了ninepatch 的底部和右侧边缘。不过,我只需要在过渡中制作第一张图像,而不是第二张。如果我以这种方式制作第二张图片,那么我的文字会从气泡中跳出,一些文本会显示在气泡的顶部和两侧。我不完全确定为什么会发生这种情况,或者为什么这个修复会起作用。但它现在可以完成这项工作。