package com.moxiu.launcher.widget.baidusb;
import com.umeng.analytics.MobclickAgent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class M_bd_MarqueeView extends HorizontalScrollView {
private int textSize;
private static int blankLength;
private static int offsetX = 0;
private LinearLayout layout;
private static M_bd_MarqueeView mequeeView;
private static int movedistance = 1;
private static boolean isLast = false;
private static int width;
private boolean isFrist = true;
private static int viewWidth;
private static int textsWidth;
private static int status = 2;
private static final int RUN = 1;
private static final int STOP = 2;
private static int speed = 20;
private MarqueeItemClickListener marqueeItemClickListener;
public interface MarqueeItemClickListener {
public void onItemClick(int position, View v);
}
private static Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
postDelayed(run, speed);
move();
};
};
private static void move() {
offsetX += movedistance;
mequeeView.smoothScrollBy(movedistance, 0);
if (isLast) {
offsetX = (textsWidth - blankLength) / 2 - width;
mequeeView.scrollTo(offsetX, 0);
isLast = false;
} else {
if (offsetX >= viewWidth) {
isLast = true;
}
}
}
private static Runnable run = new Runnable() {
@Override
public void run() {
if (status == RUN)
handler.sendEmptyMessage(10);
}
};
private Context mContext ;
public M_bd_MarqueeView(Context context) {
super(context);
// mContext = context;
// mequeeView = this;
//
// offsetX = 0;
//
// isLast = false;
//
// // textSize =<!--DVFMTSC-->16; pww
//
// textSize = 16;
//
// blankLength = getResources().getDisplayMetrics().widthPixels / 3;
//
// layout = new LinearLayout(context);
//
// layout.setOrientation(0);
//
// addView(layout);
}
public M_bd_MarqueeView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mequeeView = this;
offsetX = 0;
isLast = false;
// textSize =<!--DVFMTSC-->16; pww
textSize = 16;
blankLength = getResources().getDisplayMetrics().widthPixels / 3;
layout = new LinearLayout(context);
layout.setOrientation(0);
addView(layout);
}
public void startMarquee() {
if (status == STOP) {
width = MeasureSpec.getSize(getWidth());
int w = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
layout.measure(w, h);
int width = layout.getMeasuredWidth();
viewWidth = width - M_bd_MarqueeView.width;
textsWidth = width;
isFrist = false;
if ((textsWidth - blankLength) / 2 > M_bd_MarqueeView.width) {
status = RUN;
handler.removeCallbacks(run);
handler.postDelayed(run, 500);
} else {
layout.removeAllViews();
initLayout(texts);
}
}
}
public void stopMarquee() {
handler.removeCallbacks(run);
status = STOP;
}
public boolean isStopMarquee() {
return status == STOP;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isFrist) {
startMarquee();
isFrist = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return true;
}
private String[] texts;
public void setText(String... texts) {
layout.removeAllViews();
this.texts = texts;
initLayout(texts);
View view = new View(getContext());
view.setPadding(0, 0, 0, 0);
view.setLayoutParams(new LinearLayout.LayoutParams(blankLength,
LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(view);
initLayout(texts);
}
private void initLayout(final String... texts) {
if (texts != null)
for (int i = 0; i < texts.length; i++) {
TextView textView = new TextView(getContext());
textView.setText(texts[i]);
textView.setPadding(20, 0, 20, 0);
textView.setTextSize(textSize);
textView.setTag(i);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("click");
if (marqueeItemClickListener != null)
marqueeItemClickListener.onItemClick(
(Integer) v.getTag(), v);
try {
Intent mintent = new Intent();
mintent.setAction(Intent.ACTION_VIEW);
mintent.addCategory(Intent.CATEGORY_DEFAULT);
mintent.setData(Uri
.parse("http://m.baidu.com/s?from=1001706a&word="
+ texts[(Integer) v.getTag()]));
MobclickAgent.onEvent(mContext,
"launcher_widget_baidu_search_326");
mContext.startActivity(mintent);
} catch (Exception e) {
// TODO: handle exception
}
}
});
layout.addView(textView);
}
}
public void setMarqueeItemClickListener(
MarqueeItemClickListener marqueeItemClickListener) {
this.marqueeItemClickListener = marqueeItemClickListener;
}
public void setMarqueeSpeed(int speed2) {
speed = speed2;
}
}