

package cn.example.vstdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnFocusChangeListener,
OnClickListener {
private TextView tv_newFilm, tv_networkTV, tv_category, tv_myFavourite,
tv_application, tv_setting;
private ImageView img1, img2, img3, img4, img5, img_cursor;
private Listviews; / / Tab page list
private int offset = 0; / / animated pictures offset
private int currIndex = 0; / / this page card number
private int bmpW; / / animated image width
private View view1, view2, view3, view4, view5, view6;
private ViewPager viewPager;
private MyViewPagerAdapter adapter;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
InitImageView ();
InitViewPager ();
}
public void init () {
tv_newFilm = (TextView) findViewById (R.id.tv_newFilm);
tv_networkTV = (TextView) findViewById (R.id.tv_networkTV);
tv_category = (TextView) findViewById (R.id.tv_category);
tv_myFavourite = (TextView) findViewById (R.id.tv_myFavourite);
tv_application = (TextView) findViewById (R.id.tv_application);
tv_setting = (TextView) findViewById (R.id.tv_setting);
img1 = (ImageView) findViewById (R.id.imageView1);
img2 = (ImageView) findViewById (R.id.imageView2);
img3 = (ImageView) findViewById (R.id.imageView3);
img4 = (ImageView) findViewById (R.id.imageView4);
img5 = (ImageView) findViewById (R.id.imageView5);
img_cursor = (ImageView) findViewById (R.id.cursor);
tv_newFilm.setOnFocusChangeListener (this);
tv_networkTV.setOnFocusChangeListener (this);
tv_category.setOnFocusChangeListener (this);
tv_myFavourite.setOnFocusChangeListener (this);
tv_application.setOnFocusChangeListener (this);
tv_setting.setOnFocusChangeListener (this);
img1.setOnClickListener (this);
img2.setOnClickListener (this);
img3.setOnClickListener (this);
img4.setOnClickListener (this);
img5.setOnClickListener (this);
}
private void InitViewPager () {
viewPager = (ViewPager) findViewById (R.id.viewPager);
views = new ArrayList();
LayoutInflater inflater = getLayoutInflater ();
view1 = inflater.inflate (R.layout.newfilm, null);
view2 = inflater.inflate (R.layout.networktv, null);
view3 = inflater.inflate (R.layout.category, null);
view4 = inflater.inflate (R.layout.myfavourite, null);
view5 = inflater.inflate (R.layout.application, null);
view6 = inflater.inflate (R.layout.mysetting, null);
views.add (view1);
views.add (view2);
views.add (view3);
views.add (view4);
views.add (view5);
views.add (view6);
viewPager.setAdapter (new MyViewPagerAdapter (views));
viewPager.setCurrentItem (0);
viewPager.setOnPageChangeListener (new MyOnPageChangeListener ());
}
private void InitImageView () {
bmpW = BitmapFactory.decodeResource (getResources (),
R.drawable.title_selector). GetWidth () ;/ / Get image width
DisplayMetrics dm = new DisplayMetrics ();
getWindowManager () getDefaultDisplay () getMetrics (dm);..
int screenW = dm.widthPixels ;/ / get resolution width
offset = (screenW / 3 - bmpW) / 2 ;/ / calculate the offset
Matrix matrix = new Matrix ();
matrix.postTranslate (offset, 0);
img_cursor.setImageMatrix (matrix) ;/ / animate the initial position
}
public class MyViewPagerAdapter extends PagerAdapter {
private ListmListViews;
public MyViewPagerAdapter (ListmListViews) {
this.mListViews = mListViews;
}
@ Override
public void destroyItem (ViewGroup container, int position, Object object) {
container.removeView (mListViews.get (position));
}
@ Override
public Object instantiateItem (ViewGroup container, int position) {
container.addView (mListViews.get (position), 0);
return mListViews.get (position);
}
@ Override
public int getCount () {
return mListViews.size ();
}
@ Override
public boolean isViewFromObject (View arg0, Object arg1) {
return arg0 == arg1;
}
}
public class MyOnPageChangeListener implements OnPageChangeListener {
int one = offset * 2 + bmpW ;/ / Page Card 1 -> Page 2 offset
cardint two = one * 2 ;/ / Page Card 1 -> Page 3 card offset
public void onPageScrollStateChanged (int arg0) {
}
public void onPageScrolled (int arg0, float arg1, int arg2) {
}
public void onPageSelected (int arg0) {
Animation animation = new TranslateAnimation (one * currIndex, one
* Arg0, 0, 0) ;/ / Obviously, this is relatively simple, only one line of code.
currIndex = arg0;
animation.setFillAfter (true) ;/ / True: stop animated images in end position
animation.setDuration (300);
img_cursor.startAnimation (animation);
Toast.makeText (MainActivity.this,
"You have selected" + viewPager.getCurrentItem () + "page card",
. Toast.LENGTH_SHORT) show ();
}
}
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}
@ Override
public void onClick (View v) {
Intent intent = new Intent ();
intent.setClass (MainActivity.this, DetailActivity.class);
switch (v.getId ()) {
case R.id.imageView1:
/ / Img1.setAlpha (200);
startActivity (intent);
break;
case R.id.imageView2:
/ / Img2.setAlpha (200);
startActivity (intent);
break;
case R.id.imageView3:
/ / Img3.setAlpha (200);
startActivity (intent);
break;
case R.id.imageView4:
startActivity (intent);
break;
case R.id.imageView5:
startActivity (intent);
break;
default:
;
}
}
@ Override
public void onFocusChange (View v, boolean hasFocus) {
switch (v.getId ()) {
case R.id.tv_newFilm:
/ / Tv_newFilm.setBackgroundResource (R.drawable.select);
tv_newFilm.requestFocus ();
tv_newFilm.setAlpha (200);
tv_newFilm.setNextFocusRightId (R.id.tv_networkTV);
tv_newFilm.setNextFocusDownId (R.id.imageView1);
/ /
tv_newFilm.setNextFocusRightId (R.id.tv_networkTV);
case R.id.tv_networkTV:
tv_networkTV.requestFocus ();
tv_networkTV.setNextFocusLeftId (R.id.tv_newFilm);
tv_networkTV.setNextFocusRightId (R.id.tv_category);
case R.id.tv_category:
tv_category.requestFocus ();
tv_category.setNextFocusLeftId (R.id.tv_networkTV);
tv_category.setNextFocusRightId (R.id.tv_myFavourite);
case R.id.tv_myFavourite:
tv_myFavourite.requestFocus ();
tv_myFavourite.setNextFocusLeftId (R.id.tv_category);
tv_myFavourite.setNextFocusRightId (R.id.tv_application);
case R.id.tv_application:
tv_application.requestFocus ();
tv_application.setNextFocusLeftId (R.id.tv_myFavourite);
tv_application.setNextFocusRightId (R.id.tv_setting);
case R.id.tv_setting:
tv_setting.requestFocus ();
tv_setting.setNextFocusLeftId (R.id.tv_application);
default:
;
}
}
/ / Private class MyViewPagerAdapter extends FragmentPagerAdapter {
/ /
/ / Public MyViewPagerAdapter () {
/ / Super (getSupportFragmentManager ());
/ /}
/ /
/ / @ Override
/ / Public Fragment getItem (int pageNumber) {
/ / Switch (pageNumber) {
/ / Case 0:
/ / Return new newFilmFragment ();
/ / Case 1:
/ / Return new networkTVFragment ();
/ / Case 2:
/ / Return new categoryFragment ();
/ / Case 3:
/ / Return new myFavouriteFragment ();
/ / Case 4:
/ / Return new applicationFragment ();
/ / Case 5:
/ / Return new mysettingFragment ();
/ / Default:
/ / Return null;
/ /}
/ /}
}
This error has been running on the crash, I do not know how to solve, seeking God, you big<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
See error messages should be the place of the exception. Check BitmapFactory.decodeResource
private void InitImageView () {
bmpW = BitmapFactory.decodeResource (getResources (),
R.drawable.title_selector). GetWidth () ;/ / Get image width
DisplayMetrics dm = new DisplayMetrics ();
getWindowManager () getDefaultDisplay () getMetrics (dm);..
int screenW = dm.widthPixels ;/ / get resolution width
offset = (screenW / 3 - bmpW) / 2 ;/ / calculate the offset
Matrix matrix = new Matrix ();
matrix.postTranslate (offset, 0);
img_cursor.setImageMatrix (matrix) ;/ / animate the initial position
}
Reply:
bmpW = BitmapFactory.decodeResource (getResources (),
R.drawable.title_selector). GetWidth () ;/ / Get image width
Reported null pointer exception here, check the position title_selector put the picture right, etc.
Reply:
Just want to be viewpager sliding effect, drawable wrote that no problem ah. What is that reason? Commented tried this, no problem
Reply:
Found,,
private void InitImageView () {
img_cursor = (ImageView) findViewById (R.id.cursor);
bmpW = BitmapFactory.decodeResource (getResources (),
R.drawable.select). GetWidth () ;/ / Get image width
DisplayMetrics dm = new DisplayMetrics ();
getWindowManager () getDefaultDisplay () getMetrics (dm);..
int screenW = dm.widthPixels ;/ / get resolution width
offset = (screenW / 6 - bmpW) / 2 ;/ / calculate the offset
Matrix matrix = new Matrix ();
matrix.postTranslate (offset, 0);
img_cursor.setImageMatrix (matrix) ;/ / animate the initial position
}
Write the first line in this function, we can, in the prior written another function.
Reply:
bmpW = BitmapFactory.decodeResource (getResources (),
R.drawable.title_selector). GetWidth () ;/ / Get image width
The landlord here:
BitmapFactory.decodeResource (getResources (),
R.drawable.title_selector)
Return to this picture when decoding an empty Bitmap object, perhaps the problem is in title_selector this picture, this picture you put what?
Are xml files? Just one. Png format images?
No comments:
Post a Comment