标签: android-custom-view

ACTION_CANCEL 未触发

我实现了一个 customView 来在视图被触摸和释放时添加一些动画。当用户单击此视图时,我会启动一个 80 毫秒的小动画,从而缩小视图。

当手指抬起时,播放相同的动画(反向效果)。视觉效果很棒,只要用户不移动手指离开视图就可以正常工作。

因此,当手指离开视图区域时,我无法调用 ACTION_CANCEL ,而在我的情况下,这是播放动画所必需的。

ACTION_UP、ACTION_MOVE 和 ACTION_DOWN 被正确调用,但从未 ACTION_CANCEL

这是我的代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    switch (event.getAction()) {
        case MotionEvent.ACTION_CANCEL: {
            Animation anim = new ScaleAnimation(0.9f, 1f, 0.9f, 1f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setDuration(Utils.ANIM_CLICK_LENGHT);
            anim.setFillAfter(true);
            anim.setFillBefore(true);
            startAnimation(anim);
            break;
        }
        case MotionEvent.ACTION_DOWN: {
            Animation anim = new ScaleAnimation(1f, 0.9f, 1f, 0.9f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setDuration(Utils.ANIM_CLICK_LENGHT);
            anim.setFillAfter(true);
            anim.setFillBefore(true);
            startAnimation(anim);
            break;
        }
        case MotionEvent.ACTION_UP: {
            Animation anim = new ScaleAnimation(0.9f, 1f, 0.9f, 1f,
            Animation.RELATIVE_TO_SELF, 0.5f, …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view ontouchlistener touch-event

2
推荐指数
1
解决办法
1807
查看次数

区分 customView 和滚动视图之间的触摸事件

我在滚动视图中添加了一个 customView 和 customView。现在我想区分两个触摸事件。我的问题是当尝试滚动时,customView 也会获取触摸事件,而当我尝试在 customView 中更改时,滚动视图获取事件。

  1. 滚动时如何停止 customView 触摸事件。
  2. 当 customView 需要事件时,我们如何停止滚动触摸事件。

提前致谢

android scrollview android-custom-view touch-event

2
推荐指数
1
解决办法
967
查看次数

将自定义样式设置为 android 中自定义视图内的文本视图

我有一个自定义视图类 DoubleTextView.java 如下所示,

public class DoubleTextView extends LinearLayout {
    LinearLayout layout;
    TextView upTextView;
    TextView downTextView;
    Context mContext;

    public DoubleTextView(Context context) {

        super(context);
        mContext = context;
    }

    public DoubleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;


        String service = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(service);

        layout = (LinearLayout) li.inflate(R.layout.custom_double_text, this, true);

        upTextView = layout.findViewById(R.id.text_up);
        downTextView = layout.findViewById(R.id.text_down);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DoubleTextView);

        String upText = a.getString(R.styleable.DoubleTextView_upText);
        String downText = a.getString(R.styleable.DoubleTextView_downText);
        int upTextStyle = a.getInteger(R.styleable.DoubleTextView_styleUpText,R.style.ListOtherLightInfo);
        int downTextStyle = a.getInteger(R.styleable.DoubleTextView_styleDownText,R.style.ListOtherLightInfo);

        upText …
Run Code Online (Sandbox Code Playgroud)

java android styles android-custom-view

2
推荐指数
1
解决办法
2182
查看次数

如何制作类似于 EditText 的视图以进行 SMS 代码验证?

我想在我的 Android 应用程序中添加多个内联 EditText,用户可以在其中输入文本,然后下一个 EditText 自动获得焦点。如下图所示。我想要一个关于如何实现它的示例代码(或一个库)。在此处输入图片说明

android android-custom-view android-edittext

2
推荐指数
1
解决办法
3993
查看次数

Android:抽象自定义视图和常见布局膨胀

因此,我做了一些研究,似乎在init抽象基类的/构造函数中查看膨胀并不是真正的最佳实践。我明白这是因为基类初始值设定项发生在init派生类的 /构造函数之前。由于抽象类是非最终类,所以有一个关于块this中泄漏的不错的 IDE 消息init

这就是我所追求的:

abstract class Foo @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val myView: View

    init {
        // todo@patches fix leaking "this"
        View.inflate(context, R.layout.view_foo, this)
        myView = requireNotNull(findViewById(R.id.my_view))
    }
}
Run Code Online (Sandbox Code Playgroud)
class Bar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : Foo(context, attrs, defStyleAttr) 
Run Code Online (Sandbox Code Playgroud)

我真的不想向派生类的 init 添加任何内容,或者创建myView稍后在抽象类中设置的可为空/可变变量。

还有其他人觉得这有点令人沮丧或有什么建议吗?似乎想要从基类膨胀相同的布局并不罕见。

android android-custom-view android-view kotlin

2
推荐指数
1
解决办法
1108
查看次数

自定义视图上的 findViewById 抛出 null

在解释我的问题之前,我想说我已经看过所有相关问题,但没有一个对我有用。

我是 Android 初学者。我正在使用自定义视图类,假设 CustomView 如下所示:

class CustomView(context: Context?,attributeSet: AttributeSet) : View(context) {

    class CustomView constructor(context: Context?, attributeSet: AttributeSet){

    }
    var number = 1
}
Run Code Online (Sandbox Code Playgroud)

我在片段资源中膨胀这个视图,例如:

<com.example.myapp.CustomView
     android:id="@+id/custom_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

但现在每当我尝试获取自定义视图时,它都是空的。所有兄弟视图都工作正常。

val root = inflater.inflate(R.layout.fragment_main, container,false)
val customView: CustomView = root.findViewById(R.id.custom_view)
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误

java.lang.IllegalStateException: findViewById(R.id.custom_view) must not be null
Run Code Online (Sandbox Code Playgroud)

xml android android-custom-view illegalstateexception kotlin

2
推荐指数
1
解决办法
834
查看次数

通过在自定义视图的onDraw中设置单位矩阵来偏移画布

我在onDraw自定义视图的方法中 为我的画布设置了一个矩阵,canvas.setMatrix(matrix);然后我只使用预定义的绘制绘制一个网格:

canvas.drawRect(0,0,viewWidth,viewHeight, background);

for(int i=0; i <= nRows; i++){
    canvas.drawLine(0,i*cellHeight, nCols*cellWidth,i*cellHeight,lines);
    canvas.drawLine(i*cellWidth, 0, i*cellWidth, nRows*cellHeight, lines);
    if(i != nRows)
        canvas.drawText(""+i, i*cellWidth, (i+1)*cellHeight, text);
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,整个画布偏移了一个细胞高度的1.5倍左右.知道为什么会发生这种情况或如何解决它?因此,矩阵没有初始化,因此根据文档应该是身份.

非常感谢!

java android matrix android-custom-view android-canvas

1
推荐指数
1
解决办法
4018
查看次数

在android中自定义CheckedTextView

我正在使用我工作中不是我的代码.此代码使用CheckedTextView,执行以下操作:

...
    AlertDialog.Builder alert;
        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        switch (soundPreference.getType()) {
        case BOOLEAN:
            CheckedTextView checkedTextView = (CheckedTextView) v;
            boolean checked = !checkedTextView.isChecked();
            ((CheckedTextView) v).setChecked(checked);
            switch (soundPreference.getKey()) {
            case SOUND_ACTIVE:

                sound.setAlarmActive(checked);
                break;
...
Run Code Online (Sandbox Code Playgroud)

效果很好,问题是我想要更改复选框图形.现在它看起来像(defualt,我认为): 在此输入图像描述

我希望得到这样的东西:

在此输入图像描述

如果在xml中定义了CheckedTextView,则知道如何操作.我的问题没有在xml中定义,不知道怎么做.

我发现这个: 为ListView自定义CheckedTextView 但我不知道如何实现它

感谢任何帮助.

非常感谢你提前

android android-custom-view checkedtextview

1
推荐指数
1
解决办法
9165
查看次数

如何在Canvas后面设置CustomView的背景?

我有这样的习惯View:

public class ShadowTextView extends TextView {

...

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        final int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        final int minSize = Math.min(parentWidth, parentHeight);

        mShadow = new Paint(Paint.ANTI_ALIAS_FLAG);

        RadialGradient gradient = new RadialGradient(
                parentWidth * mCenterX,
                parentHeight * mCenterY,
                minSize * mGradientRadiusWidthPercent,
                new int[]{mStartColor, mCenterColor, mEndColor},
                null,
                android.graphics.Shader.TileMode.CLAMP);

        mShadow.setDither(true);
        mShadow.setShader(gradient);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvas.drawRect(0, 0, getWidth(), getHeight(), mShadow);

    }

...

}
Run Code Online (Sandbox Code Playgroud)

在XML格式中,我希望将它CustomView …

android background canvas android-custom-view

1
推荐指数
1
解决办法
4717
查看次数

自定义ViewPager中心当前项目,在适配器中具有自定义getPageWidth

我有一个自定义ViewPager,就像一个Carousel,我希望能够在一个上显示多个页面.为此,我们有两个解决方案:

  • 使用边距setPageMargin()方法
  • 或者我们可以使用覆盖我的ViewPager适配器的getPageWidth方法.

第一个解决方案工作得很好,但我希望能够精确确定当前页面的百分比.问题在于getPageWidth解决方案,我的当前页面在左侧移动.这是合乎逻辑的,因为她只占她以前尺寸的80%.所以我试图覆盖我的自定义ViewPager的scrollTo函数,如下所示:

 @Override
    public void scrollTo(int x, int y) {
        x = (int) (x - (getWidth() - getWidth()*getAdapter().getPageWidth(getCurrentItem()))/2);
        super.scrollTo(x, y);
    }
Run Code Online (Sandbox Code Playgroud)

它正在工作,但我不能像以前一样滚动,它只是表现得很奇怪,不要朝着好的方向前进,不要按照手指......有没有任何解决方案可以让我有一个工作滚动和居中当前页面 ?

这是我的适配器:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;

public class CarouselAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {
    private ArrayList<Entity> mData = new ArrayList<>();
    private ScaledRelativeLayout cur = null, next = null;

    private float scale;
    private MainActivity context;
    private FragmentManager fragmentManager;

    public CarouselAdapter(MainActivity context, FragmentManager fragmentManager, ArrayList<Entity> mData) {
        super(fragmentManager);
        this.fragmentManager = fragmentManager;
        this.context …
Run Code Online (Sandbox Code Playgroud)

android centering android-custom-view android-viewpager

1
推荐指数
1
解决办法
3948
查看次数