自定义控件
<com.yulin.test.MyView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:layout_marginRight="10dip"
android:background="@android:color/transparent"
android:orientation="vertical" >
<TextView
style="@style/ActivityText"
android:id="@+id/My_temprature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="40sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/My_discription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="20sp" />
<Space android:layout_width="wrap_content"
android:layout_height="40dip"/>
<TextView
style="@style/ActivityText"
android:id="@+id/My_tempraturescale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="15sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/My_winddirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="15sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/My_waternum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="15sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/My_zwx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="15sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/My_cyzs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="15sp" />
</GridLayout>
<LinearLayout
android:id="@+id/half_Myblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="0dip"
android:gravity="bottom"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="32dip"
android:background="@android:color/transparent">
<TextView
style="@style/ActivityText"
android:id="@+id/My_updatetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="10sp" />
<ProgressBar
android:id="@+id/My_progress"
android:layout_width="32dip"
android:layout_height="32dip"
android:layout_toLeftOf="@id/My_updatetime"
android:indeterminateDrawable="@drawable/progress_drawable"
android:visibility="gone"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:background="@drawable/cw_forecast_bg"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/My_tomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/icon_nodata"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
style="@style/ActivityText"
android:id="@+id/two_weekday"
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="15sp" />
<Space android:layout_width="wrap_content"
android:layout_height="50dip"/>
<TextView
style="@style/ActivityText"
android:id="@+id/two_sort"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="2"
android:textSize="15sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/two_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:gravity="center_vertical"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/My_foreday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/icon_nodata"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
style="@style/ActivityText"
android:id="@+id/three_weekday"
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15sp" />
<Space android:layout_width="wrap_content"
android:layout_height="50dip"/>
<TextView
style="@style/ActivityText"
android:id="@+id/three_sort"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="2"
android:textSize="15sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/three_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:gravity="center_vertical"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/My_fourthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/icon_nodata"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
style="@style/ActivityText"
android:id="@+id/four_weekday"
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="15sp" />
<Space android:layout_width="wrap_content"
android:layout_height="50dip"/>
<TextView
style="@style/ActivityText"
android:id="@+id/four_sort"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="2"
android:textSize="15sp" />
<TextView
style="@style/ActivityText"
android:id="@+id/four_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:gravity="center_vertical"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.yulin.test.MyView>
public class MyPaperAdapter extends PagerAdapter{
MyPresStorage mPresStorage;
private String[] mCitynames;
private MyView mDiscription;
private ReourceManager mReourceManager;
private LayoutInflater mLayoutInflater;
public MyPaperAdapter(Context context){
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mReourceManager = new ReourceManager(context);
mPresStorage = new MyPresStorage(context);
mCitynames = mPresStorage.getPreferenceCity();
}
private void fillViewPaper(String city){
mDiscription = (MyView)mLayoutInflater.inflate(R.layout.Myblock, null);
mDiscription.setTag(city);
fillMyblock(city);
}
private void fillMyblock(String city){
for (InteractiveSource source : DataService.mMySourcs) {
if (source.mCity.equals(city)) {
mDiscription.updateInfo(source,mReourceManager);
break;
}
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View)object);
}
@Override
public int getItemPosition(Object object) {
if (null==mCitynames){
return POSITION_NONE;
}else{
int pos = POSITION_NONE;
int m = 0;
for (String city:mCitynames){
if(((View)object).getTag().toString().equals(city)){
pos = m;
break;
}
m++;
}
return pos;
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
String city = mCitynames[position];
fillViewPaper(city);
try {
((ViewPager) container).addView(mDiscription);
} catch (Exception e) {
}
return mDiscription;
}
@Override
public void notifyDataSetChanged() {
mCitynames = mPresStorage.getPreferenceCity();
super.notifyDataSetChanged();
}
@Override
public int getCount() {
if (null==mCitynames){
return 0;
}
return mCitynames.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
public int getPositionByViewTag(String name){
int m = 0;
for (String city:mCitynames){
if(name.equals(city)){
break;
}
m++;
}
return m;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<LinearLayout android:orientation="horizontal"
android:gravity="center" android:measureWithLargestChild="true"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0">
<Button android:id="@+id/goto_first"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="first">
</Button>
<Button android:id="@+id/goto_last"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="last">
</Button>
</LinearLayout>
</LinearLayout>
public class Polling extends FragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mViewPager = (ViewPager) findViewById(R.id.pager);
MyPaperAdapter mPaperAdpter = new MyPaperAdapter(this);
mViewPager.setAdapter(mPaperAdpter);
mViewPager.setCurrentItem(0);
}
}
public class myView extends FrameLayout{
private Context mContext;
private TextView my_discription = null;
private TextView my_temprature = null;
private TextView my_tempraturescale = null;
private TextView my_winddirection = null;
private TextView my_updatetime = null;
private TextView my_zwx = null;
private TextView my_cyzs = null;
private TextView my_waternum = null;
private LinearLayout mTwo_layout = null;
private TextView mTwo_weekday = null;
private TextView mTwo_temp = null;
private TextView mTwo_sort = null;
private LinearLayout mThree_layout = null;
private TextView mThree_weekday = null;
private TextView mThree_temp = null;
private TextView mThree_sort = null;
private LinearLayout mFour_layout = null;
private TextView mFour_weekday = null;
private TextView mFour_temp= null;
private TextView mFour_sort = null;
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
my_discription = (TextView)findViewById(R.id.weather_discription);
my_temprature = (TextView)findViewById(R.id.weather_temprature);
my_tempraturescale = (TextView)findViewById(R.id.weather_tempraturescale);
my_winddirection = (TextView)findViewById(R.id.weather_winddirection);
my_updatetime = (TextView)findViewById(R.id.weather_updatetime);
my_zwx = (TextView)findViewById(R.id.weather_zwx);
my_cyzs = (TextView)findViewById(R.id.weather_cyzs);
my_waternum = (TextView)findViewById(R.id.weather_waternum);
mTwo_layout = (LinearLayout)findViewById(R.id.weather_tomorrow);
mTwo_weekday = (TextView)findViewById(R.id.two_weekday);
mTwo_temp = (TextView)findViewById(R.id.two_temp);
mTwo_sort = (TextView)findViewById(R.id.two_sort);
mThree_layout = (LinearLayout)findViewById(R.id.weather_foreday);
mThree_weekday= (TextView)findViewById(R.id.three_weekday);
mThree_temp = (TextView)findViewById(R.id.three_temp);
mThree_sort = (TextView)findViewById(R.id.three_sort);
mFour_layout = (LinearLayout)findViewById(R.id.weather_fourthday);
mFour_weekday = (TextView)findViewById(R.id.four_weekday);
mFour_temp = (TextView)findViewById(R.id.four_temp);
mFour_sort = (TextView)findViewById(R.id.four_sort);
}
public void updateInfo(InteractiveSource source,ReourceManager mReourceManager){
WeatherSituation condition = source.getWeatherByDay(InteractiveSource.FIRST);
if (condition.getError().getError()==SimpleError.DATANOERROR){
my_discription.setText(condition.getCondition().getCondition());
my_temprature.setText(condition.getTemperature().getTg_temperature_h());
my_tempraturescale.setText(condition.getTemperature().getTemperature());
my_winddirection.setText(condition.getWind().getDirection_d()+condition.getWind().getPower());
String format = mContext.getResources().getString(R.string.weather_updatetime_format);
String str = String.format(format, condition.getUpdateTime());// updatetime
ContentResolver cv = mContext.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,android.provider.Settings.System.TIME_12_24);
if (strTimeFormat.equals("24")) {
my_updatetime.setText(str);
} else {
SimpleDateFormat m24format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat m12format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date date1=null;
try {
date1 = m24format.parse(condition.getUpdateTime());
String my12date = m12format.format(date1);
String str12 = String.format(format, my12date);
my_updatetime.setText(str12);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
my_zwx.setText(condition.getZwx().getZwx_s());
my_cyzs.setText(condition.getChy().getChy_l());
my_waternum.setText(condition.getShidu());
condition = source.getWeatherByDay(InteractiveSource.SECOND);
mTwo_layout.setBackgroundResource(mReourceManager.
getWeatherIcon(condition.getCondition().getCondition_d()));
mTwo_weekday.setText(getWeekDayStr(condition.getWeek()));
mTwo_temp.setText(condition.getTemperature().getTemperature());
mTwo_sort.setText(condition.getCondition().getCondition());
condition = source.getWeatherByDay(InteractiveSource.THIRD);
mThree_layout.setBackgroundResource(mReourceManager.
getWeatherIcon(condition.getCondition().getCondition_d()));
mThree_weekday.setText(getWeekDayStr(condition.getWeek()));
mThree_temp.setText(condition.getTemperature().getTemperature());
mThree_sort.setText(condition.getCondition().getCondition());
condition = source.getWeatherByDay(InteractiveSource.FOURTH);
mFour_layout.setBackgroundResource(mReourceManager.
getWeatherIcon(condition.getCondition().getCondition_d()));
mFour_weekday.setText(getWeekDayStr(condition.getWeek()));
mFour_temp.setText(condition.getTemperature().getTemperature());
mFour_sort.setText(condition.getCondition().getCondition());
}
}
}
================================================================
public class TimeBlock extends LinearLayout{
private int hour_partone = -1;
private int hour_parttwo = -1;
private int minute_partone = -1;
private int minute_parttwo = -1;
private ImageView mImage_hourone;
private ImageView mImage_hourtwo;
private ImageView mImage_minuteone;
private ImageView mImage_minutetwo;
private ValueAnimator scaleAnim1;
private ValueAnimator scaleAnim2;
private final int mDelayAnimation = 400;
private Context mContext;
private Handler mHandler;
public TimeBlock(Context context,AttributeSet attri) {
super(context,attri);
mContext = context;
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case 0:
setTimeView(mImage_minutetwo,minute_parttwo);
break;
case 1:
setTimeView(mImage_minuteone,minute_partone);
break;
case 2:
setTimeView(mImage_hourtwo,hour_parttwo);
break;
case 3:
setTimeView(mImage_hourone,hour_partone);
break;
default:
super.handleMessage(msg);
}
}
};
}
private void setTimeView(final ImageView view,int number) {
Resources res = mContext.getResources();
switch(number) {
case 0:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num0));
break;
case 1:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num1));
break;
case 2:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num2));
break;
case 3:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num3));
break;
case 4:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num4));
break;
case 5:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num5));
break;
case 6:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num6));
break;
case 7:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num7));
break;
case 8:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num8));
break;
case 9:
view.setImageDrawable(res.getDrawable(R.drawable.widget_num9));
break;
default :
break;
}
scaleAnim1 = ValueAnimator.ofFloat(0.0f, 1.2f).setDuration(450);
scaleAnim2 = ValueAnimator.ofFloat(1.2f, 1.0f).setDuration(150);
initalAnimation(view ,scaleAnim1,scaleAnim2);
scaleAnim1.start();
}
public void updateTime(int hour,int minute) {
int hourpartone = hour/10;
int hourparttwo = hour - hourpartone*10;
int minutepartone = minute/10;
int minuteparttwo = minute - minutepartone*10;
if (minute_parttwo != minuteparttwo){
minute_parttwo = minuteparttwo;
mHandler.sendEmptyMessageDelayed(0, 0);
}
if (minute_partone != minutepartone){
minute_partone = minutepartone;
mHandler.sendEmptyMessageDelayed(1, mDelayAnimation);
}
if (hour_parttwo != hourparttwo){
hour_parttwo = hourparttwo;
mHandler.sendEmptyMessageDelayed(2, 2*mDelayAnimation);
}
if (hour_partone != hourpartone){
hour_partone = hourpartone;
mHandler.sendEmptyMessageDelayed(3, 3*mDelayAnimation);
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mImage_hourone = (ImageView)findViewById(R.id.hour_part_one);
mImage_hourtwo = (ImageView)findViewById(R.id.hour_part_two);
mImage_minuteone = (ImageView)findViewById(R.id.minute_part_one);
mImage_minutetwo= (ImageView)findViewById(R.id.minute_part_two);
}
private void initalAnimation(final ImageView view ,ValueAnimator animator1,final ValueAnimator animator2){
animator1.setInterpolator(new AccelerateInterpolator(1.0f));
animator1.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final float b = (Float) animation.getAnimatedValue();
view.setScaleX(b*1.0f);
view.setAlpha(b*1.0f);
view.setScaleY(b*1.0f);
}
});
animator2.setInterpolator(new DecelerateInterpolator(1.0f));
animator2.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final float b = (Float) animation.getAnimatedValue();
view.setScaleX(b*1.0f);
view.setScaleY(b*1.0f);
}
});
animator1.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
view.setScaleX(0.0f);
view.setScaleY(0.0f);
view.setAlpha(0.0f);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
view.setAlpha(1.0f);
animator2.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
}
}
<com.yulin.myview xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/weather_timeblock"
android:orientation="horizontal"
android:weightSum="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="@+id/hour_part_one"
android:layout_width="30dip"
android:layout_height="45dip"
android:scaleType="fitCenter"
android:layout_weight="1">
</ImageView>
<ImageView
android:id="@+id/hour_part_two"
android:layout_width="30dip"
android:layout_height="45dip"
android:scaleType="fitCenter"
android:layout_weight="1">
</ImageView>
<ImageView
android:layout_width="20dip"
android:layout_height="45dip"
android:src="@drawable/widget_delimiter">
</ImageView>
<ImageView
android:id="@+id/minute_part_one"
android:layout_width="30dip"
android:layout_height="45dip"
android:scaleType="fitCenter"
android:layout_weight="1">
</ImageView>
<ImageView
android:id="@+id/minute_part_two"
android:layout_width="30dip"
android:layout_height="45dip"
android:scaleType="fitCenter"
android:layout_weight="1">
</ImageView>
</com.yulin.myview>
private TimeBlock mLieaLayout;
mLieaLayout = (TimeBlock)findViewById(R.id.weather_timeblock);
mLieaLayout.updateTime(time.hour,time.minute);