标签:竖直textview 竖直seekbar 竖直view 自定义特效view 竖直
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.empty.VerticalTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:text="Happy" android:textSize="24dp" android:textColor="#00FF00" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:text="Happy" android:textSize="24dp" android:textColor="#00FF00" /> <com.example.empty.VerticalTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:text="Today" android:textSize="24dp" android:textColor="#0000FF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:text="Today" android:textSize="24dp" android:textColor="#0000FF" /> </FrameLayout>
package com.example.empty; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.widget.TextView; public class VerticalTextView extends TextView{ public VerticalTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { canvas.rotate(-90); canvas.translate(-getHeight(), 0); super.onDraw(canvas); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(getMeasuredWidth() >= getMeasuredHeight()){ setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); }else if(getMeasuredWidth() < getMeasuredHeight()){ setMeasuredDimension(getMeasuredHeight(), getMeasuredHeight()); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(h, w, oldh, oldw); } }只想Copy代码的,到这里就结束了。百分百有效
@Override protected void onDraw(Canvas canvas) { canvas.rotate(-90); canvas.translate(-getHeight(), 0); super.onDraw(canvas); }很明显,这里我们什么都没做,只将我们的Canvas画布先旋转逆时针90度,然后再将我们的Canvas画布像下移动了我们整个View的高度。理论就是这么简单。
void android.graphics.Canvas.rotate(float arg0)
android:layout_width="wrap_content" android:layout_height="wrap_content"区别在这里,为什么同样是wrap_content,他们的画布不一样
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(getMeasuredWidth() >= getMeasuredHeight()){ setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); }else if(getMeasuredWidth() < getMeasuredHeight()){ setMeasuredDimension(getMeasuredHeight(), getMeasuredHeight()); } }代码很简单,但是如果你不理解onMeasure函数,我又要很为难。
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(h, w, oldh, oldw); }怎么样是不是很简单。
<com.jeason.mediaplayer.view.VerticalSeekBar android:id="@+id/light_vertical_seekbar" android:layout_width="wrap_content" android:layout_height="160dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dip" android:paddingLeft="6dip" android:paddingRight="6dip" android:progressDrawable="@drawable/progress_holo_light" android:thumb="@drawable/detail_icon_schedule_ball" android:visibility="gone" />
package com.jeason.mediaplayer.view; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.Display; import android.view.MotionEvent; import android.widget.SeekBar; public class VerticalSeekBar extends SeekBar{ @SuppressWarnings("unused") private int ScreenHeight; @SuppressWarnings("deprecation") public VerticalSeekBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub Activity mContext =(Activity)context; Display display = mContext.getWindowManager().getDefaultDisplay(); ScreenHeight = display.getHeight(); } protected void onDraw(Canvas c) { //将SeekBar转转90度 c.rotate(-90); //将旋转后的视图移动回来 c.translate(-getHeight(),0); super.onDraw(c); } @Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(heightMeasureSpec, widthMeasureSpec); setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(h, w, oldh, oldw); } @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: int i=0; //获取滑动的距离 i=getMax() - (int) (getMax() * event.getY() / getHeight()); //设置进度 setProgress(i); onSizeChanged(getWidth(), getHeight(), 0, 0); break; case MotionEvent.ACTION_CANCEL: break; } return true; } @Override public void setProgress(int progress){ super.setProgress(progress); onSizeChanged(getWidth(), getHeight(), 0, 0); } }
标签:竖直textview 竖直seekbar 竖直view 自定义特效view 竖直
原文地址:http://blog.csdn.net/jaysong2012/article/details/45504289