Java tutorial
/* * Copyright (c) 2012 Wireless Designs, LLC * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.mb.kids_mind.view; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Point; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import com.mb.kids_mind.fragment.SingleSketchMenu; import com.mb.kids_mind.listener.PageChagedListener; /** * PagerContainer: A layout that displays a ViewPager with its children that are outside * the typical pager bounds. */ public class PagerContainer extends FrameLayout implements ViewPager.OnPageChangeListener { private ViewPager mPager; boolean mNeedsRedraw = false; private int currentPage; ImageView img; SharedPreferences pref; Context pContext; public PagerContainer(Context context) { super(context); pContext = context; init(); } public PagerContainer(Context context, AttributeSet attrs) { super(context, attrs); init(); } public PagerContainer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //Disable clipping of children so non-selected pages are visible setClipChildren(false); //Child clipping doesn't work with hardware acceleration in Android 3.x/4.x //You need to set this value here if using hardware acceleration in an // application targeted at these releases. setLayerType(View.LAYER_TYPE_SOFTWARE, null); } @Override protected void onFinishInflate() { try { mPager = (ViewPager) getChildAt(0); mPager.setOnPageChangeListener(this); } catch (Exception e) { throw new IllegalStateException("The root child of PagerContainer must be a ViewPager"); } } public ViewPager getViewPager() { return mPager; } private Point mCenter = new Point(); private Point mInitialTouch = new Point(); @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { mCenter.x = w / 2; mCenter.y = h / 2; } @Override public boolean onTouchEvent(MotionEvent ev) { //We capture any touches not already handled by the ViewPager // to implement scrolling from a touch outside the pager bounds. switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mInitialTouch.x = (int) ev.getX(); mInitialTouch.y = (int) ev.getY(); default: ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y); break; } return mPager.dispatchTouchEvent(ev); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //Force the container to redraw on scrolling. //Without this the outer pages render initially and then stay static if (mNeedsRedraw) invalidate(); } private static final String TAG = "MainActivity"; public static PageChagedListener listene; int prefIndex = 0; int oldWidth = 320, oldHeight = 320, newWidth = 400, newHeight = 400; @Override public void onPageSelected(int position) { Log.v(TAG, "poposisition" + position); if (PagerContainer.listene != null) { // Log.v(d.getTaga()," "+color+""); PagerContainer.listene.onPageChange(position); } // SingleSketchMenu.listener=new ViewSizeChangedListener() { // // @Override // public void onPageChange(ImageView img) { // // TODO Auto-generated method stub // // LayoutParams params = (LayoutParams) img.getLayoutParams(); // params.width = newWidth; // params.height=newHeight; // // existing height is ok as is, no need to edit it // img.setLayoutParams(params); // //// LayoutParams Oldparams = (LayoutParams) img.getLayoutParams(); //// params.width = newWidth; //// params.height=newHeight; //// // existing height is ok as is, no need to edit it //// img.setLayoutParams(params); // //// Log.v(TAG,"pagechaged"); //// View a=v.getChildAt(position); //// Log.v(TAG,"pagechaged"); //// FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(newWidth,newHeight); //// Log.v(TAG,"pagechaged"); //// a.setLayoutParams(params); //// Log.v(TAG,"pagechaged"); //// View oldV =v.getChildAt(prefIndex); //// Log.v(TAG,"pagechaged"); //// FrameLayout.LayoutParams oldParams =new FrameLayout.LayoutParams(oldWidth,oldHeight); //// Log.v(TAG,"pagechaged"); //// oldV.setLayoutParams(oldParams); //// prefIndex=position; // } // }; // if(img!=null){ // switch(position){ // case 0: // img.setImageResource(R.drawable.re_dotor1); // break; // case 1:img.setImageResource(R.drawable.re_dotor2); // break; // case 2: // img.setImageResource(R.drawable.re_dotor2); // break; // case 3: // img.setImageResource(R.drawable.re_dotor1); // break; // } // } } public int getCurrentPage() { return currentPage; } @Override public void onPageScrollStateChanged(int state) { mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE); } }