android 自己定义TextView"会发脾气的TextView"
转载请注明出处王亟亟的大牛路
Git上看到的一个自己定义控件就搞来研究研究。蛮可爱的。
项目结构:
执行效果:非常Q谈。谈的图片什么都 都能够换哦
自己定义View:
public class JelloToggle extends FrameLayout {
private static final int DEFAULT_DURATION = 1000;//动画持续时间
private static final int UNCHECKED_JELLO_COLOR = 0xffadadad;//初始化颜色
private static final int CHECKED_JELLO_COLOR = 0xffff0000;//初始化颜色
private Rect mJelloRect;
private Paint mJelloPaint;
private Scroller mScroller;
private Path mJelloPath;
private TimeInterpolator mInterpolator;
private OnCheckedChangeListener mListener;
private Drawable mCheckedDrawable;
private Drawable mOnCheckDrawable;
private Drawable mUnCheckedDrawable;
private Drawable mDrawable;
private boolean mChecked = false;
private int mTouchStartX;
private int mScrollOffset;
private int mJelloSize;
private int mDragLimit;
private int mJelloMax;
private int mJelloOffset;
private long mStartTime;
private long mDuration;
private int mCheckedColor = CHECKED_JELLO_COLOR;
public JelloToggle(Context context) {
super(context);
init();
}
public JelloToggle(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public JelloToggle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mJelloPaint = new Paint();
mJelloPaint.setAntiAlias(true);
mCheckedDrawable = getResources().getDrawable(R.drawable.checked);
mOnCheckDrawable = getResources().getDrawable(R.drawable.check_on);
mUnCheckedDrawable = getResources().getDrawable(R.drawable.uncheck);
setJelloState();
mJelloRect = new Rect();
mScroller = new Scroller(getContext());
mJelloPath = new Path();
mInterpolator = new EaseOutElasticInterpolator();
mDuration = DEFAULT_DURATION;
}
private void calPath() {
mJelloPath.rewind();
mJelloPath.moveTo(mJelloRect.right, 0);
mJelloPath.lineTo(mJelloRect.left, 0);
mJelloPath.cubicTo(mJelloRect.left, mJelloSize / 2, mJelloRect.left + mJelloOffset -
mJelloSize / 3, mJelloSize * 3 / 4,
mJelloRect.left, mJelloSize);
mJelloPath.lineTo(mJelloRect.right, mJelloRect.bottom);
mJelloPath.close();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mDragLimit = getMeasuredWidth() / 4;
mJelloSize = getMeasuredHeight();
mJelloRect.set(getMeasuredWidth() - mJelloSize, 0, getMeasuredWidth() + mDragLimit,
mJelloSize);
mCheckedDrawable.setBounds(mJelloRect.left, mJelloRect.top, mJelloRect.left + mJelloSize,
mJelloSize);
mOnCheckDrawable.setBounds(mJelloRect.left, mJelloRect.top, mJelloRect.left + mJelloSize,
mJelloSize);
mUnCheckedDrawable.setBounds(mJelloRect.left, mJelloRect.top, mJelloRect.left + mJelloSize,
mJelloSize);
calPath();
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.save();
canvas.translate(mScrollOffset, 0);
super.dispatchDraw(canvas);
canvas.restore();
canvas.save();
canvas.translate(mScrollOffset / 2, 0);
canvas.drawPath(mJelloPath, mJelloPaint);
mDrawable.draw(canvas);
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean ret = true;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.forceFinished(true);
}
mTouchStartX = (int) event.getX();
mDrawable = mOnCheckDrawable;
break;
case MotionEvent.ACTION_MOVE:
int dragLen = Math.min(0, (int) event.getX() - mTouchStartX);
mScrollOffset = Math.max(-mDragLimit, dragLen);
mJelloOffset = dragLen;
calPath();
postInvalidate();
break;
case MotionEvent.ACTION_UP:
if (mScrollOffset < 0) {
mScroller.startScroll(mScrollOffset, 0, -mScrollOffset, 0);
mJelloMax = mJelloOffset;
if (mJelloOffset <= -mDragLimit) {
mChecked = !mChecked;
if (mListener != null) {
mListener.onCheckedChange(mChecked);
}
}
setJelloState();
postInvalidate();
startJello();
}
break;
}
return ret;
}
private void setJelloState() {
if (mChecked) {
mJelloPaint.setColor(mCheckedColor);
mDrawable = mCheckedDrawable;
} else {
mJelloPaint.setColor(UNCHECKED_JELLO_COLOR);
mDrawable = mUnCheckedDrawable;
}
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
mScrollOffset = mScroller.getCurrX();
ViewCompat.postInvalidateOnAnimation(this);
}
}
public void setJelloDuration(long duration) {
if (duration <= 0) {
duration = DEFAULT_DURATION;
}
mDuration = duration;
}
public void setCheckedJelloColor(int color) {
mCheckedColor = color;
setJelloState();
postInvalidate();
}
public void setCheckedDrawable(Drawable drawable) {
mCheckedDrawable = drawable;
}
public void setOnCheckDrawable(Drawable drawable) {
mOnCheckDrawable = drawable;
}
public void setUnCheckedDrawable(Drawable drawable) {
mUnCheckedDrawable = drawable;
}
private void startJello() {
mStartTime = AnimationUtils.currentAnimationTimeMillis();
post(mJelloRunnable);
}
private Runnable mJelloRunnable = new Runnable() {
@Override
public void run() {
long playTime = AnimationUtils.currentAnimationTimeMillis() - mStartTime;
if (playTime < mDuration) {
float fraction = playTime / (float) mDuration;
mJelloOffset = (int) (mJelloMax * (1 - mInterpolator.getInterpolation
(fraction)));
calPath();
ViewCompat.postInvalidateOnAnimation(JelloToggle.this);
post(this);
} else {
mJelloOffset = 0;
calPath();
ViewCompat.postInvalidateOnAnimation(JelloToggle.this);
}
}
};
public interface OnCheckedChangeListener {
void onCheckedChange(boolean checked);
}
public void setCheckedChangeListener(OnCheckedChangeListener listener) {
mListener = listener;
}
}
主Activity
public class MainActivity extends ActionBarActivity {
private JelloToggle mToggle1;
private JelloToggle mToggle2;
private JelloToggle mToggle3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToggle1 = (JelloToggle) findViewById(R.id.jello1);
mToggle1.setCheckedJelloColor(0xffdb654a);
mToggle2 = (JelloToggle) findViewById(R.id.jello2);
mToggle2.setCheckedJelloColor(0xfffb008a);
mToggle3 = (JelloToggle) findViewById(R.id.jello3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:text="Title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"/>
<me.fichardu.jellotoggle.JelloToggle
android:id="@+id/jello1"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="24dp"
android:text="Text"
android:textSize="20sp"
android:textColor="@android:color/white"
android:background="#f0007D8B"
/>
</me.fichardu.jellotoggle.JelloToggle>
<TextView
android:text="Title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginTop="50dp"/>
<me.fichardu.jellotoggle.JelloToggle
android:id="@+id/jello2"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="24dp"
android:text="Text"
android:textSize="20sp"
android:textColor="@android:color/white"
android:background="#ff6D4C41"
/>
</me.fichardu.jellotoggle.JelloToggle>
<me.fichardu.jellotoggle.JelloToggle
android:id="@+id/jello3"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="24dp"
android:text="Text"
android:textSize="20sp"
android:textColor="@android:color/white"
android:background="#ff43A047"
/>
</me.fichardu.jellotoggle.JelloToggle>
</LinearLayout>
详细内容能够看源代码。拿来就能用。想知道怎么实现能够 看源代码。量不大
源代码地址:http://yunpan.cn/cd4szcyz5LsT4 訪问password fb75
android 自己定义TextView"会发脾气的TextView"的更多相关文章
- Android 自己定义TextView 实现文本间距
转载请标明出处: http://blog.csdn.net/u011974987/article/details/50845269: Android系统中TextView默认显示中文时会比較紧凑.不是 ...
- Android 高级UI设计笔记05:使用TextView实现跑马灯的效果
1. 使用TextView属性实现跑马灯的效果: (1). 新建一个Android工程,命名为"MarqueeTextViewDemo",如下: (2). 来到activity_m ...
- Android UI--自定义ListView(实现下拉刷新+加载更多)
Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...
- android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)
自定义一个dialog: 之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗? ...
- ANDROID自己定义视图——onLayout源代码 流程 思路具体解释
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候.事实上非常easy.仅仅须要知道3步骤: 1.測量- ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- android 自己定义视频播放器之2/1
非常久没更新博客,相信大家年后都比較忙. 今天给大家带来了一款视频播放器,首先确认的得有几点. 1.首先得有个播放视频的view. 2.加点额外功能进去左边上下滑动调节亮度,右边上下滑动调节声量: 3 ...
- Android自己定义button实现长按功能
Android自己定义button实现长按功能 通过自己定义BUTTON,写一个LongTouchBtn类,在按下的时候运行onTouchEvent事件,通过这个事件使用回调函数来实现长按功能! XM ...
- Android自己定义View的实现方法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17357967 不知不觉中,带你一步步深入了解View系列的文章已经写到第四篇了.回 ...
随机推荐
- spark 数据倾斜的一些表现
spark 数据倾斜的一些表现 https://yq.aliyun.com/articles/62541
- 【Android】Android6.0读取通话记录
需求:读取通话记录,然后列表显示,每条记录的数据包括姓名.号码.类型(来电.去电.未接,字体颜色分别为绿.蓝.红),然后长按条目弹出一个列表弹窗,显示[复制号码到拨号盘].[发短信].[打电话]. 先 ...
- 批量修改mysql数据库引擎
数据库表中部分是MyISAM引擎,部分是InnoDB.由于MyISAM不支持事务,所以需要全部修改为InnoDB引擎. (下面例子SQL语句中 ecsoft 为数据库名) 通过以下查询可以看到数据库中 ...
- shell+钉钉机器人完成java程序中断后自启动和实时监控
java实时程序在运行过程中偶尔出现异常信息中断的情况,通过shell脚本即可完成自启动. 以下为监控一个实时的java程序的shell脚本. 通过每10秒检查一次java程序的进程,来判断程序是否处 ...
- Casual Note of OS
20170104 冯诺依曼计算机(遵循冯诺依曼结构设计的计算机:存储器.运算器.控制器.输入设备.输出设备)之前也有计算机,不过在那之前的计算机是专用的,不可编程,只能干特定的事情没法干其他事.与之前 ...
- jquery跨域访问解决方案
客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1.2以后跨域问题便迎刃而解.由于自己在项目中遇到跨域问题,借此机会对跨域问题来刨根问底,查阅了相关资料和自己的实践,算 ...
- [hadoop读书笔记] 第十章 管理Hadoop集群
P375 Hadoop管理工具 dfsadmin - 查询HDFS状态信息,管理HDFS. bin/hadoop dfsadmin -help 查询HDFS基本信息 fsck - 检查HDFS中文件的 ...
- phpc.sinaapp.com 加密的解密方法
很简单,用类似phpjm的解密方式,替换掉_inc.php中最后一个return中的eval为print就出来了. http://www.cnblogs.com/lonelyxmas/p/458285 ...
- Numpy的array数组和标量之间的运算
矢量化 数组很重要,因为它使你不用编写循环即可对数据执行批量运算.这通常就叫做矢量化(vectorzation) 数组与数组的运算 数组与标量的算术运算
- 对微软Microsoft Dynamics CRM 的认识
MS CRM的认识 技术层面: MS CRM使用了当前最为流行的Web Service作为数据交互的手段,这给我们的二次开发和系统级的集成带来了无可比拟的方便性.易用性.我们不用关心如何去访问CRM数 ...