本文主要是介绍仿htc布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//-------------------------lock_test.xml--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="330dip"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="420dip"
android:layout_marginTop="-20dip"
android:id="@+id/up"
>
<ImageView
android:layout_marginTop="130dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:background="@drawable/lockscreen">
</ImageView>
<TextView
android:layout_marginTop="135dip"
android:id="@+id/cmcc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
</TextView>
<!-- pictrue of unlock -->
<LinearLayout
android:id="@+id/linear"
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/cmcc">
<ImageView
android:layout_marginTop="12dip"
android:layout_width="17px"
android:layout_height="17px"
android:src="@drawable/icon_lock">
</ImageView>
<TextView
android:id="@+id/havelock"
android:layout_marginTop="5dip"
android:layout_marginLeft="8dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="22dip"
android:text="@string/havelock"
>
</TextView>
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_below="@id/cmcc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="45dip"
android:layout_marginTop="3dip">
</TextView>
<TextView
android:id="@+id/date"
android:layout_marginTop="-1dip"
android:layout_below="@id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textStyle="bold"
>
</TextView>
<!--第二行 -->
<ImageView
android:id="@+id/left"
android:layout_marginLeft="40dip"
android:layout_marginTop="10dip"
android:layout_below="@id/image"
android:layout_alignParentLeft="true"
android:layout_width="18dip"
android:layout_height="30dip"
android:src="@drawable/arrseven">
</ImageView>
<TextView
android:id="@+id/drag_info"
android:layout_width="wrap_content"
android:layout_below="@id/image"
android:layout_marginTop="10dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/drag_info"/>
<ImageView
android:id="@+id/right"
android:layout_marginRight="40dip"
android:layout_marginTop="10dip"
android:layout_below="@id/image"
android:layout_alignParentRight="true"
android:layout_width="18dip"
android:layout_height="30dip"
android:src="@drawable/arreight"
>
</ImageView>
</RelativeLayout>
</LinearLayout>
//----------------------------------------SlidingTab.java---------------------------------------------------------
package com.android.internal.widget;
import com.android.internal.widget.SlidingTab_htc.OnTriggerListener;
import com.android.internal.R;
import android.content.Context;
import android.graphics.Color;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlidingTab_htc extends LinearLayout {
RelativeLayout up;
ImageView left, right,image;
LinearLayout linear;
TextView havelock, cmcc, time, date, drag_info,ampm;//add by xzg_006
private float down_y = 0;
private int deltaY = 0;
private boolean mTracking = false;
private static final long VIBRATE_SHORT = 30;
private static final long VIBRATE_LONG = 40;
private OnTriggerListener mOnTriggerListener;
private int mGrabbedState = OnTriggerListener.NO_HANDLE;
private boolean mTriggered = false;
private Vibrator mVibrator;
private static final int ANIM_DURATION = 350;
private static final int start_y = 90;
private static final int TARGET_LENGHT = 90;
private boolean ISCALLING = false;
public SlidingTab_htc(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("xia", "SlidingTab_htc---SlidingTab_htc()");
// TODO Auto-generated constructor stub
final LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.lock_test, this, true);
}
//add by xzg_004 for bug_id null to htc_lockscreen phonenummber begin
public void setCalling(String phonenummber){
time.setVisibility(View.VISIBLE);
time.setTextSize(24);
time.setText(phonenummber);
}
//add by xzg_004 for bug_id null to htc_lockscreen phonenummber end
public void setCalling(boolean flag){
ISCALLING = flag;
if(flag){
image.setBackgroundResource(R.drawable.lockscreen_phone);
drag_info.setText(R.string.draginfo);
cmcc.setVisibility(View.INVISIBLE);
time.setVisibility(View.INVISIBLE);
date.setVisibility(View.INVISIBLE);
linear.setVisibility(View.INVISIBLE);
drag_info.setVisibility(View.VISIBLE);
left.setVisibility(View.VISIBLE);
right.setVisibility(View.VISIBLE);
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Log.i("xia", "SlidingTab_htc---onFinishInflate()");
up = (RelativeLayout) this.findViewById(R.id.up);// whole
linear = (LinearLayout) this.findViewById(R.id.linear);// lock info
cmcc = (TextView) this.findViewById(R.id.cmcc);
time = (TextView) this.findViewById(R.id.time);
havelock = (TextView) this.findViewById(R.id.havelock);
date = (TextView) this.findViewById(R.id.date);
ampm = (TextView) this.findViewById(R.id.ampm);//add by xzg_006
drag_info = (TextView) this.findViewById(R.id.drag_info);
left = (ImageView) this.findViewById(R.id.left);
image = (ImageView) this.findViewById(R.id.image);
right = (ImageView) this.findViewById(R.id.right);
cmcc.setTextColor(Color.WHITE);
time.setTextColor(Color.WHITE);
havelock.setTextColor(Color.WHITE);
date.setTextColor(Color.WHITE);
diplaytime(true);
}
public void settimedate(String timestr,String datestr){
Log.i("xia","--SlidingTab_htc----settimedate---------");
// modified by xzg_006 begin
String stra[] = timestr.split(" ");
Log.i("xia","------"+stra[0]);
time.setText(stra[0]);
date.setText(datestr);
if(stra.length>=2)
{
Log.i("xia","------"+stra[1]);
ampm.setText(stra[1]);
}
Log.i("xia","--settimedate end---");
}
// modified by xzg_006 end
public void setCmcc(String timestr){
cmcc.setText(timestr);
}
public interface OnTriggerListener {
public static final int NO_HANDLE = 0;
public static final int LEFT_HANDLE = 1;
public static final int RIGHT_HANDLE = 2;
void onTrigger(View v, int whichHandle);
void onGrabbedStateChange(View v, int grabbedState);
}
public void setRightTabResources(int iconId, int targetId, int barId,
int tabId) {
Log.i("xia", "SlidingTab_htc---setRightTabResources()");
}
public void setLeftHintText(int resId) {
Log.i("xia", "SlidingTab_htc---setLeftHintText()");
}
public void setLeftTabResources(int iconId, int targetId, int barId,
int tabId) {
Log.i("xia", "SlidingTab_htc---setLeftTabResources()");
// mLeftSlider.setIcon(iconId);
// // mLeftSlider.setTarget(targetId);
// mLeftSlider.setBarBackgroundResource(barId);
// mLeftSlider.setTabBackgroundResource(tabId);
// mLeftSlider.updateDrawableStates();
}
@Override
public void setVisibility(int visibility) {
Log.i("xia", "SlidingTab_htc---setVisibility(visibility)"+visibility);
if(visibility==0)
{
deltaY = 0;
up.setPadding(0, 0, 0, 0);
}
if (visibility != getVisibility() && visibility == View.INVISIBLE) {
reset(false);
}
super.setVisibility(visibility);
}
public void resetViews() {
up.setPadding(0, 0, 0, 0);
diplaytime(true);
}
void diplaytime(boolean flag) {
if(!ISCALLING){
if (flag) {
cmcc.setVisibility(View.VISIBLE);
time.setVisibility(View.VISIBLE);
date.setVisibility(View.VISIBLE);
ampm.setVisibility(View.VISIBLE); //rgk_20111202_xzg_lockscreen_shangwu_kejian
linear.setVisibility(View.INVISIBLE);
drag_info.setVisibility(View.INVISIBLE);
left.setVisibility(View.INVISIBLE);
right.setVisibility(View.INVISIBLE);
} else {
cmcc.setVisibility(View.INVISIBLE);
time.setVisibility(View.INVISIBLE);
date.setVisibility(View.INVISIBLE);
ampm.setVisibility(View.INVISIBLE); //rgk_20111202_xzg_lockscreen_shangwu_BUkejian
linear.setVisibility(View.VISIBLE);
drag_info.setVisibility(View.VISIBLE);
left.setVisibility(View.VISIBLE);
right.setVisibility(View.VISIBLE);
}
}
}
// public SlidingTab_htc(Context context, AttributeSet attrs) {
// super(context, attrs);
// }
public void setDragInfo(String DragInfo){
drag_info.setVisibility(View.VISIBLE);
drag_info.setText(DragInfo);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
Log.i("xia", " 33 onInterceptTouchEvent");
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
Log.i("xia", " x = " + x + " y = " + y);
// if (mAnimating) {
// return false;
// }
// mLeftSlider.display_date(false);
boolean leftHit = y > 0 && y < 250;
Log.i("xia", " leftHit = " + leftHit);
if (!mTracking && !leftHit) {
Log.i("xia", "aaaaaaaaaaaa return fallse ");
return false;
}
switch (action) {
case MotionEvent.ACTION_DOWN: {
Log.i("xia", "bbbbbbbbbbbb ACTION_DOWN ");
down_y = y;
diplaytime(false);
mTracking = true;
// mTriggered = false;
// vibrate(VIBRATE_SHORT);
// mCurrentSlider = mLeftSlider;
// mThreshold = isHorizontal() ? THRESHOLD : 1.0f - THRESHOLD;
// setGrabbedState(OnTriggerListener.LEFT_HANDLE);
// mCurrentSlider.setState(Slider.STATE_PRESSED);
break;
}
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mTracking) {
final int action = event.getAction();
final float y = event.getY();
switch (action) {
case MotionEvent.ACTION_MOVE:
Log.i("xia", " 44 MotionEvent.ACTION_MOVE");
if (y > 0 && y < 330) {
moveHandle(y);
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Log.i("xia", " 44 MotionEvent.ACTION_CANCEL");
if(!ISCALLING){
boolean thresholdReached;
thresholdReached = deltaY > 95;
if (thresholdReached) {
Log.i("xia", "====reset(true)=====");
reset(true);
dispatchTriggerEvent(OnTriggerListener.LEFT_HANDLE);
setGrabbedState(OnTriggerListener.NO_HANDLE);
} else {
Log.i("xia", "====reset(false)=====");
reset(false);
setGrabbedState(OnTriggerListener.NO_HANDLE);
}
}else{
if(deltaY > 90){
reset(true);
dispatchTriggerEvent(OnTriggerListener.LEFT_HANDLE);
setGrabbedState(OnTriggerListener.NO_HANDLE);
}else if(deltaY < -80){
reset();
dispatchTriggerEvent(OnTriggerListener.RIGHT_HANDLE);
setGrabbedState(OnTriggerListener.NO_HANDLE);
}else
{
reset(false);
setGrabbedState(OnTriggerListener.NO_HANDLE);
}
}
mTracking = false;
break;
}
}
return mTracking || super.onTouchEvent(event);
}
public void reset(boolean flag) {
if (deltaY != 0) {
if (!flag) {
Log.i("xia", " deltaY " + deltaY);
up.setPadding(0, 0, 0, 0);
TranslateAnimation trans = new TranslateAnimation(0, 0, deltaY,
0);
trans.setDuration(ANIM_DURATION);
trans.setFillAfter(false);
trans.setAnimationListener(mAnimationDoneListener);
up.startAnimation(trans);
} else {
Log.i("xia", " deltaY " + deltaY);
up.setPadding(0, deltaY + 50, 0, -(deltaY + 50));
TranslateAnimation trans = new TranslateAnimation(0, 0, -50, 0);
trans.setDuration(ANIM_DURATION);
trans.setFillAfter(false);
trans.setAnimationListener(mAnimationDoneListener);
up.startAnimation(trans);
}
}
deltaY = 0;
}
public void reset() {
Log.i("xia", " deltaY " + deltaY);
// up.setPadding(0, deltaY - 50, 0, -(deltaY - 50));
// TranslateAnimation trans = new TranslateAnimation(0, 0, 50, 0);
// trans.setDuration(ANIM_DURATION);
// trans.setFillAfter(false);
// trans.setAnimationListener(mAnimationDoneListener);
// up.startAnimation(trans);
deltaY = 0;
}
private void moveHandle(float y) {
if (!ISCALLING) {
if (y > down_y && y > start_y) {
if (down_y < start_y) {
deltaY = (int) (y - start_y);
} else {
deltaY = (int) (y - down_y);
}
up.setPadding(0, deltaY, 0, -(deltaY));
}
}
else{
deltaY = (int) (y - down_y);
up.setPadding(0, deltaY, 0, -(deltaY));
}
// TODO: be more conservative about what we're invalidating
}
private final AnimationListener mAnimationDoneListener = new AnimationListener() {
// @Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
onAnimationDone();
}
// @Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
// @Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
};
void onAnimationDone() {
up.setPadding(0, 0, 0, 0);
diplaytime(true);
}
public void setRightHintText(int resId) {
Log.i("xia", "SlidingTab_htc---setRightHintText()");
}
public void setHoldAfterTrigger(boolean holdLeft, boolean holdRight) {
Log.i("xia", "SlidingTab_htc---setHoldAfterTrigger()");
// mHoldLeftOnTransition = holdLeft;
// mHoldRightOnTransition = holdRight;
}
/**
* Triggers haptic feedback.
*/
private synchronized void vibrate(long duration) {
if (mVibrator == null) {
mVibrator = (android.os.Vibrator) getContext().getSystemService(
Context.VIBRATOR_SERVICE);
}
mVibrator.vibrate(duration);
}
public void setOnTriggerListener(OnTriggerListener listener) {
Log.i("xia", "SlidingTab_htc---setOnTriggerListener()");
mOnTriggerListener = listener;
}
/**
* Dispatches a trigger event to listener. Ignored if a listener is not set.
*
* @param whichHandle
* the handle that triggered the event.
*/
private void dispatchTriggerEvent(int whichHandle) {
Log.i("xia", "SlidingTab_htc---dispatchTriggerEvent(whichHandle=" + whichHandle + ")");// invork
// call
// lockscreen
// method
vibrate(VIBRATE_LONG);
if (mOnTriggerListener != null) {
mOnTriggerListener.onTrigger(this, whichHandle);
}
}
/**
* Sets the current grabbed state, and dispatches a grabbed state change
* event to our listener.
*/
private void setGrabbedState(int newState) {
Log.i("xia", "SlidingTab_htc---setGrabbedState(wint newState)");
if (newState != mGrabbedState) {
mGrabbedState = newState;
if (mOnTriggerListener != null) {
mOnTriggerListener.onGrabbedStateChange(this, mGrabbedState);
}
}
}
}
这篇关于仿htc布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!