转载请注明出处王亟亟的大牛路

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&quot;会发脾气的TextView&quot;的更多相关文章

  1. Android 自己定义TextView 实现文本间距

    转载请标明出处: http://blog.csdn.net/u011974987/article/details/50845269: Android系统中TextView默认显示中文时会比較紧凑.不是 ...

  2. Android 高级UI设计笔记05:使用TextView实现跑马灯的效果

    1. 使用TextView属性实现跑马灯的效果: (1). 新建一个Android工程,命名为"MarqueeTextViewDemo",如下: (2). 来到activity_m ...

  3. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  4. android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)

    自定义一个dialog: 之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗? ...

  5. ANDROID自己定义视图——onLayout源代码 流程 思路具体解释

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候.事实上非常easy.仅仅须要知道3步骤: 1.測量- ...

  6. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  7. android 自己定义视频播放器之2/1

    非常久没更新博客,相信大家年后都比較忙. 今天给大家带来了一款视频播放器,首先确认的得有几点. 1.首先得有个播放视频的view. 2.加点额外功能进去左边上下滑动调节亮度,右边上下滑动调节声量: 3 ...

  8. Android自己定义button实现长按功能

    Android自己定义button实现长按功能 通过自己定义BUTTON,写一个LongTouchBtn类,在按下的时候运行onTouchEvent事件,通过这个事件使用回调函数来实现长按功能! XM ...

  9. Android自己定义View的实现方法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17357967 不知不觉中,带你一步步深入了解View系列的文章已经写到第四篇了.回 ...

随机推荐

  1. Fiddler工具非常强大好用

    传递一个json对象发post请求案例: 1.打开Fiddler,点击Composer选项卡 2.下拉框选择Post 3.输入请求的URL,比如:http://localhost:49194/api/ ...

  2. django 返回json数据

    from django.core import serializers @login_required def ajax_get_data(request): json_data = serializ ...

  3. Pyperclip – A cross-platform clipboard module for Python

    Usage is simple: import pyperclip pyperclip.copy('The text to be copied to the clipboard.') spam = p ...

  4. JAVA-JSP动作元素之plugin、params、fallback

    相关资料:<21天学通Java Web开发> 结果总结:1.<jsp:plugin>.<jsp:params>.<jsp:fallback>三个动作元素 ...

  5. ILOG JRules 和 WebSphere Process Server 集成概述

    ILOG JRules 和 WebSphere Process Server 集成概述 简介 业务流程管理(Business Process Management,BPM)和业务规则管理系统(Busi ...

  6. 基于thinkphp的在线编辑器kindeditor-v4.1.3

    首先,去官网下载最新版的kindeditor,然后把里面asp,jsp,net,example的全删除,然后改名为editor放进public(最外层目录的public)文件夹里面. 在目录lib目录 ...

  7. web前端开发与iOS终端开发的异同[转]

    * {-webkit-tap-highlight-color: rgba(0,0,0,0);}html {-webkit-text-size-adjust: none;}body {font-fami ...

  8. 火狐FireFox看视频不能全屏显示的问题

    问题:最大化/全屏时显示有问题,不能全屏显示,只能看到左上角一部分画面. 解决办法:在画面右键弹出的菜单中选择[设置]——勾选[启用硬件加速].

  9. Go Revel - main函数分析

    运行revel命令时,首先会编译整个项目,在编译时,会根据`app.conf`配置文件生成两个源码文件`tmp/main.go`.`routes/routes.go`,其中`main.go`是整个项目 ...

  10. hbase源码系列(六)HMaster启动过程

    这一章是server端开始的第一章,有兴趣的朋友先去看一下hbase的架构图,我专门从网上弄下来的. 按照HMaster的run方法的注释,我们可以了解到它的启动过程会去做以下的动作. * <l ...