【问题标题】:Convert Android Custom ImageView to Xamarin.Android将 Android 自定义 ImageView 转换为 Xamarin.Android
【发布时间】:2017-09-02 16:35:19
【问题描述】:

我正在尝试通过使用 Xamarin.Android 扩展 Android 核心 ImageView 类来创建自定义 ImageView 类。下面是一段 Java 代码和一个不完整的 C# 实现。我需要最后两种方法的帮助。

安卓 JAVA 代码

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

public class IconViewView extends ImageView {

private ColorStateList tint;

public IconView(Context context) {
  super(context);
}

public IconView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context, attrs, 0);
}

public IconView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init(context, attrs, defStyle);
}

private void init(Context context, AttributeSet attrs, int defStyle) {
  TypedArray a = context.obtainStyledAttributes(
  attrs, R.styleable.IconView, defStyle, 0);
  tint = a.getColorStateList(R.styleable.IconView_iconTint);
  a.recycle();
}

@Override
protected void drawableStateChanged() {
  super.drawableStateChanged();
  if (tint != null && tint.isStateful()) {
    updateTintColor();
  }
}

public void setColorFilter(ColorStateList tint) {
  this.tint = tint;
  super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}

private void updateTintColor() {
  int color = tint.getColorForState(getDrawableState(), 0);
  setColorFilter(color);
}

} 

ANDROID XAMARIN C# 代码

using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Support.V4.Content;
using Android.Support.V4.Graphics.Drawable;
using Android.Util;
using Android.Widget;

namespace Example.Droid.App.Views
{
    public class IconView : ImageView
    {
        private ColorStateList tint;
        private Context context;
        public IconView(Context context) :base(context)
    {
        Initialize(context, null, 0);
    }

    public IconView(Context context, IAttributeSet attrs) :
        base(context, attrs)
    {
        Initialize(context, attrs, 0);
    }

    public IconView(Context context, IAttributeSet attrs, int defStyle) :
        base(context, attrs, defStyle)
    {
        Initialize(context, attrs, defStyle);
    }

    void Initialize(Context mContext, IAttributeSet attrs, int defStyle)
    {
        context = mContext;
        TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.IconView, defStyle, 0);
        tint = a.GetColorStateList(Resource.Styleable.IconView_iconTint);
        a.Recycle();
    }

    protected override void DrawableStateChanged()
    {
        base.DrawableStateChanged();
        if (tint != null && tint.IsStateful)
            UpdateTintColor();
    }

    private void UpdateTintColor() {
        /* I NEED HELP HERE */
    }
    public void SetColorFilter(ColorStateList tint) {
        /* I NEED HELP HERE */
    }

   }
}

我需要一些有关 Xamarin C# 中这些方法的帮助

private void UpdateTintColor() {
    /* I NEED HELP HERE */
}
public void SetColorFilter(ColorStateList tint) {
    /* I NEED HELP HERE */
}

【问题讨论】:

  • 任何被覆盖的方法都需要添加override关键字,一般可以在Java使用super的地方使用C#的base关键字。
  • 谢谢杰森。这里有什么。 private void UpdateTintColor() { /* I NEED HELP HERE */ } public void SetColorFilter(ColorStateList tint) { /* I NEED HELP HERE */ }

标签: java c# android xamarin


【解决方案1】:

使用new Color(),当您需要将int 转换为Color 时,它就会重载。

https://developer.xamarin.com/api/constructor/Android.Graphics.Color.Color/p/System.Int32/

private void UpdateTintColor()
{
    var color = new Color(tint.GetColorForState(GetDrawableState(), new Color(0)));
    SetColorFilter(color);
}
public void SetColorFilter(ColorStateList tint)
{
    this.tint = tint;
    base.SetColorFilter(new Color(tint.GetColorForState(GetDrawableState(), new Color(0))));
}

这里有多个关于这个问题的未解决的错误:

https://bugzilla.xamarin.com/show_bug.cgi?id=36396#c3 https://bugzilla.xamarin.com/show_bug.cgi?id=57521

请在此问题上自行抄送并添加评论以描述在没有扩展方法的情况下使用此 API 的困难,我们将努力改进这些。

【讨论】:

  • 非常感谢。太棒了。
【解决方案2】:

我终于让它工作了。我扩展了 AppCompatImageView 并感谢 Jon Douglas。这是我的代码

1. App/View/IconView.cs

using System;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Support.V4.Content;
using Android.Support.V4.Graphics.Drawable;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Widget;

namespace Example.Droid.App.Views
{
    public class IconView : AppCompatImageView
    {
        private ColorStateList tint;
        private Context context;
        public IconView(Context context) :base(context)
    {
        Initialize(context, null, 0);
    }

    public IconView(Context context, IAttributeSet attrs) :
        base(context, attrs)
    {
        Initialize(context, attrs, 0);
    }

    public IconView(Context context, IAttributeSet attrs, int defStyle) :
        base(context, attrs, defStyle)
    {
        Initialize(context, attrs, defStyle);
    }

    void Initialize(Context mContext, IAttributeSet attrs, int defStyle)
    {
        context = mContext;
        TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.IconView, defStyle, 0);
        tint = a.GetColorStateList(Resource.Styleable.IconView_iconTint);
        a.Recycle();
    }

    protected override void DrawableStateChanged()
    {
        base.DrawableStateChanged();
        if (tint != null && tint.IsStateful)
            UpdateTintColor();
    }

   private void UpdateTintColor()
    {
        var color = new Color(tint.GetColorForState(GetDrawableState(), new Color(0)));
        SetColorFilter(color);
    }

    public void SetColorFilter(ColorStateList tint)
    {
        this.tint = tint;
        base.SetColorFilter(new Color(tint.GetColorForState(GetDrawableState(), new Color(0))));
    }

   }
}

2。 Resources/layout/activity_example.axml 的一部分

<Example.Droid.App.Views.IconView
 android:layout_width="30dp"
 android:layout_height="30dp"
 app:srcCompat="@drawable/ic_camera"
 android:layout_margin="5dp"
 android:duplicateParentState="true"
 app:iconTint="@color/primary_selector"/>

3.资源/颜色/primary_selector.xml

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true"
      android:color="#FFFFFF"/> <!-- pressed -->
  <item android:state_selected="true"
      android:state_focused="true"
      android:color="#FFFFFF"/> <!-- focused -->
  <item android:color="#CCCCCC"/> <!-- default -->
</selector>

4.资源/值/attrs.xml

<?xml version="1.0" encoding="UTF-8" ?>
  <resources>
   <declare-styleable name="IconView">
    <attr name="iconTint" format="reference|color" />
   </declare-styleable>
  </resources>

5.资源/drawable/ic_camera.xml

<?xml version="1.0" encoding="UTF-8" ?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M4,4H7L9,2H15L17,4H20A2,2 0 0,1 22,6V18A2,2 0 0,1 20,20H4A2,2 0 0,1 2,18V6A2,2 0 0,1 4,4M12,7A5,5 0 0,0 7,12A5,5 0 0,0 12,17A5,5 0 0,0 17,12A5,5 0 0,0 12,7M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9Z" />

干杯...

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多