package com.varunest.sparkbutton;
import com.varunest.sparkbutton.custom.TypedAttrUtils;
import com.varunest.sparkbutton.helper.CircleComponent;
import com.varunest.sparkbutton.helper.DotsComponent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.utils.Color;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.multimodalinput.event.TouchEvent;
import java.io.IOException;
import java.io.InputStream;
/**
* SparkButton
*
* @author varun 7th July 2016
*/
public class SparkButton extends DirectionalLayout implements Component.ClickedListener, Component.TouchEventListener {
private static final int INVALID_RESOURCE_ID = -1;
private static final float DOTVIEW_SIZE_FACTOR = 3;
private static final float DOTS_SIZE_FACTOR = .1f;
private static final float CIRCLEVIEW_SIZE_FACTOR = 1.4f;
public int imageResourceIdActive = INVALID_RESOURCE_ID;
public int imageResourceIdInactive = INVALID_RESOURCE_ID;
public int imageSize = 30;
public int dotsSize;
public int circleSize;
public int secondaryColor;
public int primaryColor;
public int activeImageTint;
public int inActiveImageTint;
public DotsComponent dotsComponent;
public CircleComponent circleComponent;
public Image imageView;
public boolean isPressOnTouch = true;
public float animationSpeed = 1;
public boolean isChecked = false;
private AnimatorGroup animatorSet;
private SparkEventListener listener;
private String imageFlag;
/**
* SparkButton
*
* @param context context
*/
public SparkButton(Context context) {
super(context);
}
/**
* SparkButton
*
* @param context context
* @param attrs attrs
*/
public SparkButton(Context context, AttrSet attrs) {
super(context, attrs);
geneticist(attrs);
init();
}
/**
* SparkButton
*
* @param context context
* @param attrs attrs
* @param defStyleAttr defStyleAttr
*/
public SparkButton(Context context, AttrSet attrs, String defStyleAttr) {
super(context, attrs, defStyleAttr);
geneticist(attrs);
init();
}
/**
* setColors
*
* @param startColor startColor
* @param endColor endColor
*/
public void setColors(int startColor, int endColor) {
this.secondaryColor = startColor;
this.primaryColor = endColor;
updateView();
}
private void updateView() {
circleComponent.setColors(secondaryColor, primaryColor);
dotsComponent.setColors(secondaryColor, primaryColor);
}
/**
* setAnimationSpeed
*
* @param animationSpeed animationSpeed
*/
public void setAnimationSpeed(float animationSpeed) {
this.animationSpeed = animationSpeed;
}
/**
* init
*/
void init() {
circleSize = (int) (imageSize * CIRCLEVIEW_SIZE_FACTOR);
dotsSize = (int) (imageSize * DOTVIEW_SIZE_FACTOR);
DirectionalLayout parse = (DirectionalLayout) LayoutScatter.getInstance(
getContext()).parse(ResourceTable.Layout_layout_spark_button, this, true);
circleComponent = (CircleComponent) parse.findComponentById(ResourceTable.Id_vCircle);
circleComponent.setWidth(circleSize);
circleComponent.setHeight(circleSize);
circleComponent.setColors(secondaryColor, primaryColor);
dotsComponent = (DotsComponent) parse.findComponentById(ResourceTable.Id_vDotsView);
dotsComponent.setColors(secondaryColor, primaryColor);
dotsComponent.setMaxDotSize((int) (imageSize * DOTS_SIZE_FACTOR));
dotsComponent.setWidth(dotsSize);
dotsComponent.setHeight(dotsSize);
imageView = (Image) parse.findComponentById(ResourceTable.Id_ivImage);
imageView.setHeight(imageSize);
imageView.setWidth(imageSize);
imageView.setPixelMap(imageResourceIdInactive);
imageView.setClickedListener(this);
imageView.setTouchEventListener(this);
}
/**
* Call this function to start spark animation
*/
public void playAnimation() {
if (animatorSet != null) {
animatorSet.cancel();
} else {
animatorSet = new AnimatorGroup();
}
initAnimation();
}
private void initAnimation() {
AnimatorProperty starScaleAnimator = imageView.createAnimatorProperty();
starScaleAnimator.scaleXFrom(0.2f).scaleYFrom(0.2f).scaleYBy(0.8f).scaleXBy(0.8f).setDuration(500);
AnimatorValue dotsAnimator = new AnimatorValue();
dotsAnimator.setDuration(500);
dotsAnimator.setCurveType(Animator.CurveType.LINEAR);
dotsAnimator.setValueUpdateListener((animatorValue, v) -> dotsComponent.setCurrentProgress(v));
AnimatorValue innerCircleAnimator = new AnimatorValue();
innerCircleAnimator.setDuration(500);
innerCircleAnimator.setCurveType(Animator.CurveType.LINEAR);
innerCircleAnimator.setValueUpdateListener((animatorValue, v) -> {
circleComponent.setInnerCircleRadiusProgress(v);
circleComponent.setOuterCircleRadiusProgress(v);
});
animatorSet.runParallel(innerCircleAnimator, starScaleAnimator, dotsAnimator);
if (isChecked || !isPressOnTouch) {
animatorSet.start();
} else {
imageView.setScaleX(1);
imageView.setScaleY(1);
}
initListener();
}
private void initListener() {
animatorSet.setStateChangedListener(new Animator.StateChangedListener() {
@Override
public void onStart(Animator animator) {
if (listener != null) {
listener.onEvent(imageView, isChecked);
}
}
@Override
public void onStop(Animator animator) {
}
@Override
public void onCancel(Animator animator) {
circleComponent.setInnerCircleRadiusProgress(0);
circleComponent.setOuterCircleRadiusProgress(0);
dotsComponent.setCurrentProgress(0);
imageView.setScaleX(1);
imageView.setScaleY(1);
}
@Override
public void onEnd(Animator animator) {
if (listener != null) {
listener.onEvent(imageView, isChecked);
}
}
@Override
public void onPause(Animator animator) {
}
@Override
public void onResume(Animator animator) {
}
});
}
/**
* Returns whether the button is checked (Active) or not.
*
* @return boolean
*/
public boolean isChecked() {
return isChecked;
}
/**
* Change Button State (Works only if both active and disabled image resource is defined)
*
* @param isFlag desired checked state of the button
*/
public void setChecked(boolean isFlag) {
isChecked = isFlag;
imageView.setPixelMap(getPixelMap(isChecked ? imageResourceIdActive : imageResourceIdInactive));
}
/**
* setImageFlag
*
* @param flag flag
*/
public void setImageFlag(String flag) {
this.imageFlag = flag;
}
/**
* setEventListener
*
* @param listener listener
*/
public void setEventListener(SparkEventListener listener) {
this.listener = listener;
}
/**
* pressOnTouch
*
* @param isPressOnTouch pressOnTouch
*/
public void pressOnTouch(boolean isPressOnTouch) {
this.isPre
没有合适的资源?快使用搜索试试~ 我知道了~
拥有与Twitter心跳动画类似效果的按钮
共54个文件
png:16个
java:15个
xml:9个
需积分: 5 0 下载量 25 浏览量
2024-05-01
10:06:04
上传
评论
收藏 587KB ZIP 举报
温馨提示
拥有与Twitter心跳动画类似效果的按钮
资源推荐
资源详情
资源评论
收起资源包目录
SparkButton-master.zip (54个子文件)
SparkButton-master
image
gif1.gif 488KB
app
src
test
java
com
varunest
sample
ExampleTest.java 181B
ohosTest
java
com
varunest
sample
ExampleOhosTest.java 650B
config.json 784B
main
resources
base
media
ic_star_on.png 3KB
ic_thumb.png 8KB
ic_twitter.png 5KB
ic_star_off.png 2KB
ic_heart_on.png 2KB
ic_heart_off.png 5KB
icon.png 7KB
ic_github.png 4KB
ic_launcher.png 4KB
element
string.json 2KB
color.json 692B
graphic
button_shape.xml 317B
background_ability_main.xml 176B
layout
demo_heart.xml 2KB
ability_demo.xml 1KB
demo_twitter.xml 2KB
github_button.xml 901B
demo_facebook.xml 2KB
demo_star.xml 3KB
java
com
varunest
sample
MyApplication.java 271B
ScreenPageSlideProvider.java 6KB
MainAbility.java 388B
DemoAbilitySlice.java 7KB
config.json 1KB
build.gradle 631B
LICENSE 11KB
CHANGELOG.md 242B
build.gradle 917B
settings.gradle 32B
sparkbutton
src
test
java
com
varunest
sparkbutton
ExampleTest.java 186B
main
resources
base
media
ic_star_on.png 3KB
ic_thumb.png 8KB
ic_twitter.png 5KB
ic_star_off.png 2KB
ic_heart_on.png 2KB
ic_heart_off.png 5KB
ic_github.png 4KB
element
string.json 108B
color.json 922B
layout
layout_spark_button.xml 764B
java
com
varunest
sparkbutton
helper
Utils.java 1KB
CircleComponent.java 5KB
DotsComponent.java 8KB
SparkEventListener.java 716B
SparkButtonBuilder.java 2KB
custom
TransceiverUtil.java 3KB
TypedAttrUtils.java 3KB
SparkButton.java 12KB
config.json 465B
build.gradle 634B
共 54 条
- 1
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6804
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功