Android学习日记(yzy):集成布局和滑动效果的实现

来源:互联网 发布:易语言盗号源码2016 编辑:程序博客网 时间:2024/06/08 08:13

把之前写的简单的集成布局写上去,以免自己又忘记了。初步实现了正确的登陆后向下滑动界面即可转向另一个activity,其实我首先想到的是实现类似Launcher的开锁类型,即只用一个activity,并创建一个全屏的dialog,当实现效果后dismiss这个dialog,但是,在dialog中自定义布局的滑动一直报错,太麻烦,因此将就一下,实现转到另一个activity。


首先是一个登陆表单的布局集成。

public class AVlidationView extends LinearLayout {    final static private String TAG="AVlidationView";    private Context mContext;    private EditText mItemInputUserName;    private EditText mItemInputPassword;    private ImageView mUserNameIcon;    private ImageView mPasswordIcon;    private Bitmap mUserNameIconBitmap;    private Bitmap mPasswordIconBitmap;    private String mUserNameHint;    private String mPasswordHint;    private String mUserName = "yzy";    private String mPassword = "123";    private boolean mGudgeLogin = false;    public AVlidationView(Context context){        super(context);        initAVlidationView();    }    public AVlidationView(Context context, AttributeSet attrs){        super(context,attrs);        this.mContext = context;        initAVlidationattrs(attrs);        initAVlidationView();    }    private void initAVlidationattrs(AttributeSet attrs){        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.SimpleItemInputForm);        mUserNameIconBitmap = BitmapFactory.decodeResource(getResources(),                a.getResourceId(R.styleable.SimpleItemInputForm_username_icon,R.mipmap.login_user_icon));        mPasswordIconBitmap = BitmapFactory.decodeResource(getResources(),                a.getResourceId(R.styleable.SimpleItemInputForm_password_icon,R.mipmap.login_password_icon));        mUserNameHint = a.getString(R.styleable.SimpleItemInputForm_username_hint);        mPasswordHint = a.getString(R.styleable.SimpleItemInputForm_password_hint);        a.recycle();    }    private void initAVlidationView(){        LayoutInflater.from(mContext).inflate(R.layout.avlidation_view,this,true);        mItemInputUserName = (EditText)findViewById(R.id.item_input_username);        mItemInputPassword = (EditText)findViewById(R.id.item_input_password);        mUserNameIcon = (ImageView)findViewById(R.id.username_icon);        mPasswordIcon = (ImageView)findViewById(R.id.password_icon);        mUserNameIcon.setImageBitmap(mUserNameIconBitmap);        mPasswordIcon.setImageBitmap(mPasswordIconBitmap);        getGudgeContent();    }    public boolean getGudgeContent(){        if (mItemInputUserName.getText().toString().equals(mUserName)                && mItemInputPassword.getText().toString().equals(mPassword)){            mGudgeLogin = true;        }else {            mGudgeLogin = false;        }        return  mGudgeLogin;    }


其次,在此之上实现滑动效果的实现,当然也可以写在上面的自定义布局,不过会显得代码很臃肿,并且难以修改。因此,我又写在另一个自定义布局,并且建立了一个监听接口,来以向下滑动实现button效果。

/** * Created by Administrator on 2016/9/1. */public class ScrollLoginView extends FrameLayout {    private Context mContext;    private int fTouchDownY;    private int fPreTouchMoveY ;    private int fTouchMoveY ;    private int fTouchUpY;    private int offsetY;    private int longOffsetY = 0 ;    private AVlidationView aVlidationView;    private boolean fDestroyLockScreen = false ;    private OnEventUpListener mEventUp;    public void setOnEventUpListener(OnEventUpListener eventUp){        this.mEventUp = eventUp;    }    public interface OnEventUpListener{        public void onEventUp();    }    public ScrollLoginView(Context context){        super(context);        initScrollLoginView();    }    public ScrollLoginView(Context context, AttributeSet attrs){        super(context,attrs);        this.mContext = context;        initScrollLoginView();    }    private void initScrollLoginView(){        LayoutInflater.from(mContext).inflate(R.layout.scroll_login_view,this,true);        aVlidationView = (AVlidationView)findViewById(R.id.avlidation_view);    }    public boolean onTouchEvent(MotionEvent event){        int action = event.getAction();        switch (action){            case MotionEvent.ACTION_DOWN:                fTouchDownY = fPreTouchMoveY = (int)event.getRawY();                handleEventDOWN();                break;            case MotionEvent.ACTION_MOVE:                fTouchMoveY = (int)event.getRawY();                offsetY = fTouchMoveY - fPreTouchMoveY ;                fPreTouchMoveY = fTouchMoveY;                handleEventMOVE();                break;            case MotionEvent.ACTION_UP:                fPreTouchMoveY =  fTouchUpY = (int)event.getRawY();                handleEventUP();                mEventUp.onEventUp();                break;        }        return true;    }    private void handleEventDOWN(){    }    private void handleEventMOVE(){        if(offsetY> 0 && longOffsetY < 10) {            scrollBy(0, -offsetY);            longOffsetY += offsetY;        } else if(longOffsetY > 10){            scrollBy(0, -offsetY);            longOffsetY += offsetY;        }    }    private void handleEventUP(){        if (longOffsetY > 800                ){            fDestroyLockScreen = true;        }else {            fDestroyLockScreen = false;        }        scrollTo(0,0);        longOffsetY = 0;    }    public boolean getGudgeContent() {        if (fDestroyLockScreen) {            return aVlidationView.getGudgeContent();        }        return false;    }}
最后将效果实现在Activity中:

public class LoginActivity extends Activity {    private ScrollLoginView mScrollLoginView;    private boolean correctUser;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        mScrollLoginView = (ScrollLoginView)findViewById(R.id.scroll_login_view);        mScrollLoginView.setOnEventUpListener(new ScrollLoginView.OnEventUpListener() {            @Override            public void onEventUp() {                Intent intent = new Intent(LoginActivity.this,MainActivity.class);                correctUser = mScrollLoginView.getGudgeContent();                if(correctUser){                    startActivity(intent);                }            }        });    }}


转到的activity:

public class MainActivity extends FragmentActivity {    private ViewPager viewPager;    private List<Fragment> fragmentList;    private FragmentAdapter mFragmentAdapter;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initFragment();    }    private void initFragment(){        fragmentList = new ArrayList<Fragment>();        viewPager = (ViewPager)findViewById(R.id.m_viewpager);        fragmentList.add(new MainFragment() );        fragmentList.add(new WeatherForecastFragment());        mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(),fragmentList);        viewPager.setAdapter(mFragmentAdapter);    }

当然,添加一个attrs.xml文件是必须的:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="SimpleItemInputForm">        <attr name="username_icon" format="reference"></attr>        <attr name="password_icon" format="reference"></attr>        <attr name="username_hint" format="string"></attr>        <attr name="password_hint" format="string"></attr>    </declare-styleable></resources>



0 0