翻译自java示例https://raw.githubusercontent.com/Xjasz/AndroidZoomableViewGroup/master/ZoomListView.java

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using System.Collections.Generic;
using Android.Net;
using Android.Graphics;
using System.Net;
using Android.Content;
using Android.Util;
using Java.Lang; namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); // Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main); var listView1 = FindViewById<ListView>(Resource.Id.listView1);
var rows = new List<RowItem>();
rows.Add(new RowItem { URL = "http://192.168.0.50:8001/OMS/tempimage/c152fd1f-ef44-42a6-96fb-3b4894ab8004636499813730162442.gif" });
rows.Add(new RowItem { URL = "http://192.168.0.50:8001/OMS/tempimage/9e8a1d81-7211-4c1f-bc5c-b7193c12a92c636499813730162442.gif" });
rows.Add(new RowItem { URL = "http://192.168.0.50:8001/OMS/tempimage/6f0eca83-7b5e-4e9e-999c-3ef88e277117636499813730162442.gif" });
listView1.Adapter = new ListViewRowAdapter(this, rows);
}
} public class MyListView : ListView
{
private MyListViewParameter ParameterInfo = new MyListViewParameter();
private ScaleGestureDetector ScaleDetector { get; set; } public MyListView(Context context)
:base(context)
{
this.ScaleDetector = new ScaleGestureDetector(context, new ScaleListener(ParameterInfo, this));
}
public MyListView(Context context, IAttributeSet attrs)
:base(context,attrs)
{
this.ScaleDetector = new ScaleGestureDetector(context, new ScaleListener(ParameterInfo, this));
}
public MyListView(Context context, IAttributeSet attrs, int defStyleAttr)
:base(context,attrs,defStyleAttr)
{
this.ScaleDetector = new ScaleGestureDetector(context, new ScaleListener(ParameterInfo, this));
}
public MyListView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes)
:base(context,attrs,defStyleAttr,defStyleRes)
{
this.ScaleDetector = new ScaleGestureDetector(context, new ScaleListener(ParameterInfo, this));
} protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
this.ParameterInfo.Width = MeasureSpec.GetSize(widthMeasureSpec);
this.ParameterInfo.Height = MeasureSpec.GetSize(heightMeasureSpec);
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
} public override bool OnTouchEvent(MotionEvent ev)
{
var result = base.OnTouchEvent(ev);
var action = ev.Action;
this.ScaleDetector.OnTouchEvent(ev);
switch (action)
{
case MotionEventActions.Down:
{
float x = ev.GetX();
float y = ev.GetY(); this.ParameterInfo.LastTouchX = x;
this.ParameterInfo.LastTouchY = y; this.ParameterInfo.ActivePointerId = ev.GetPointerId();
break;
} case MotionEventActions.Move:
{
int pointerIndex = ev.FindPointerIndex(this.ParameterInfo.ActivePointerId);
float x = ev.GetX(pointerIndex);
float y = ev.GetY(pointerIndex);
float dx = x - this.ParameterInfo.LastTouchX;
float dy = y - this.ParameterInfo.LastTouchY; this.ParameterInfo.PosX += dx;
this.ParameterInfo.PosY += dy; if (this.ParameterInfo.PosX > 0.0f)
this.ParameterInfo.PosX = 0.0f;
else if (this.ParameterInfo.PosX < this.ParameterInfo.MaxWidth)
this.ParameterInfo.PosX = this.ParameterInfo.MaxWidth; if (this.ParameterInfo.PosY > 0.0f)
this.ParameterInfo.PosY = 0.0f;
else if (this.ParameterInfo.PosY < this.ParameterInfo.MaxHeight)
this.ParameterInfo.PosY = this.ParameterInfo.MaxHeight; this.ParameterInfo.LastTouchX = x;
this.ParameterInfo.LastTouchY = y; this.Invalidate();
break;
} case MotionEventActions.Up:
{
this.ParameterInfo.ActivePointerId = this.ParameterInfo.INVALID_POINTER_ID;
break;
} case MotionEventActions.Cancel:
{
this.ParameterInfo.ActivePointerId = this.ParameterInfo.INVALID_POINTER_ID;
break;
} case MotionEventActions.PointerUp:
{
int pointerIndex = ((int)action & (int)MotionEventActions.PointerIndexMask) >> (int)MotionEventActions.PointerIndexShift;
int pointerId = ev.GetPointerId(pointerIndex);
if (pointerId == this.ParameterInfo.ActivePointerId)
{
int newPointerIndex = pointerIndex == ? : ;
this.ParameterInfo.LastTouchX = ev.GetX(newPointerIndex);
this.ParameterInfo.LastTouchY = ev.GetY(newPointerIndex);
this.ParameterInfo.ActivePointerId = ev.GetPointerId(newPointerIndex);
}
break;
}
} return true;
} protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
canvas.Save(SaveFlags.Matrix);
canvas.Translate(this.ParameterInfo.PosX, this.ParameterInfo.PosY);
canvas.Scale(this.ParameterInfo.ScaleFactor, this.ParameterInfo.ScaleFactor);
canvas.Restore(); } protected override void DispatchDraw(Canvas canvas)
{
canvas.Save(SaveFlags.Matrix);
if (this.ParameterInfo.ScaleFactor == 1.0f)
{
this.ParameterInfo.PosX = 0.0f;
this.ParameterInfo.PosY = 0.0f;
}
canvas.Translate(this.ParameterInfo.PosX, this.ParameterInfo.PosY);
canvas.Scale(this.ParameterInfo.ScaleFactor, this.ParameterInfo.ScaleFactor);
base.DispatchDraw(canvas);
canvas.Restore();
this.Invalidate();
} public class MyListViewParameter
{
public MyListViewParameter()
{
this.INVALID_POINTER_ID = -;
this.ActivePointerId = -;
this.ScaleFactor = 1.0f;
this.MaxWidth = 0.0f;
this.MaxHeight = 0.0f;
} public int INVALID_POINTER_ID { get; private set; }
public int ActivePointerId { get; set; }
public float ScaleFactor { get; set; }
public float MaxWidth { get; set; }
public float MaxHeight { get; set; }
public float LastTouchX { get; set; }
public float LastTouchY { get; set; }
public float PosX { get; set; }
public float PosY { get; set; }
public float Width { get; set; }
public float Height { get; set; }
} private class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener
{
private MyListViewParameter ParameterInfo { get; set; }
private ListView MyListView { get; set; } public ScaleListener(MyListViewParameter pi,ListView lv)
{
this.ParameterInfo = pi;
this.MyListView = lv;
} public override bool OnScale(ScaleGestureDetector detector)
{
this.ParameterInfo.ScaleFactor *= detector.ScaleFactor;
this.ParameterInfo.ScaleFactor = Math.Max(1.0f, Math.Min(this.ParameterInfo.ScaleFactor, 3.0f));
this.ParameterInfo.MaxWidth = this.ParameterInfo.Width - (this.ParameterInfo.Width * this.ParameterInfo.ScaleFactor);
this.ParameterInfo.MaxHeight = this.ParameterInfo.Height - (this.ParameterInfo.Height * this.ParameterInfo.ScaleFactor);
this.MyListView.Invalidate();
return true;
}
}
} public class RowItem
{
public string URL { get; set; }
} public class ListViewRowAdapter : BaseAdapter<RowItem>
{
List<RowItem> items;
Activity context;
public ListViewRowAdapter(Activity context, List<RowItem> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override RowItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
} /// <summary>
/// 下载图片
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private Bitmap GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null; using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > )
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, , imageBytes.Length);
}
} return imageBitmap;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.ListViewRow, null);
var url = Uri.Parse(item.URL);
view.FindViewById<ImageView>(Resource.Id.Image1).SetImageBitmap(GetImageBitmapFromUrl(item.URL));
return view;
}
}
}

xamarin C# 安卓实现 ListView 放大缩小的更多相关文章

  1. PhotoView实现图片随手势的放大缩小的效果

    项目需求:在listView的条目中如果有图片,点击条目,实现图片的放大,并且图片可以根据手势来控制图片放大缩小的比例.类似于微信朋友圈中查看好友发布的照片所实现的效果. 思路是这样的:当点击条目的时 ...

  2. 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果

    首先呢,还是一贯作风,我们先来看看众多应用中的示例:(这种效果是很常见的,可以说应用的必须品.)                搜狐客户端                               ...

  3. 猫猫学IOS(二)UI之button操作 点击变换 移动 放大缩小 旋转

    不多说,先上图片看效果,猫猫分享.必须精品 原创文章.欢迎转载.转载请注明:翟乃玉的博客 地址:viewmode=contents">http://blog.csdn.net/u013 ...

  4. AJ学IOS(02)UI之按钮操作 点击变换 移动 放大缩小 旋转

    不多说,先上图片看效果,AJ分享,必须精品 这个小程序主要实现点击方向键可以让图标上下左右动还有放大缩小以及旋转的功能,点击图片会显示另一张图片. 点击变换 其实用到了按钮的两个状态,再State C ...

  5. 【开源】专业K线绘制[K线主副图、趋势图、成交量、滚动、放大缩小、MACD、KDJ等)

    这是一个iOS项目雅黑深邃的K线的绘制. 实现功能包括K线主副图.趋势图.成交量.滚动.放大缩小.MACD.KDJ,长按显示辅助线等功能 预览图 最后的最后,这是项目的开源地址:https://git ...

  6. 鼠标上下滑动总是放大缩小页面,按住ctrl+0

    鼠标上下滑动总是放大缩小页面,可能是ctrl键失灵了,幸好键盘有两个ctrl键,按住ctrl+0,页面就正常了,吓死宝宝了,~~~~(>_<)~~~~

  7. WPF布局之让你的控件随着窗口等比放大缩小,适应多分辨率满屏填充应用

    一直以来,我们设计windows应用程序,都是将控件的尺寸定好,无论窗体大小怎么变,都不会改变,这样的设计对于一般的应用程序来说是没有问题的,但是对于一些比较特殊的应用,比如有背景图片的,需要铺面整个 ...

  8. C# GDI绘制矩形框,鼠标左键拖动可移动矩形框,滚轮放大缩小矩形框

    最近工作需要,要做一个矩形框,并且 用鼠标左键拖动矩形框移动其位置.网上查了一些感觉他们做的挺复杂的.我自己研究一天,做了一个比较简单的,发表出来供大家参考一下.如觉得简单,可路过,谢谢.哈哈. 先大 ...

  9. Android 本地/网路下载图片实现放大缩小

     Android 本地加载/网路下载图片实现放大缩小拖拉效果,自定义控件. package com.example.ImageViewCustom; import android.app.Activi ...

随机推荐

  1. WIN8外包公司—长年承接WIN8(surface)应用外包—北京动点飞扬软件

    WIN8外包公司—长年承接WIN8(surface)应用外包 一.我们长年专门承接WIN8外包.Surface外包. WPF 外包.HTML5外包.WindowsPhone 外包.Silverligh ...

  2. webpack引入eslint详解

  3. haoop fs 命令

    Hadoop fs 命令详解   参考文档:https://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-common/FileSy ...

  4. 【文献08】多移动机器人轨迹跟踪控制系统设计与实现-运动学ADRC控制

    参考: 陈俊, 俞立, 滕游. 多移动机器人轨迹跟踪控制系统设计与实现[J]. 计算机测量与控制, 2017(7). https://drive.wps.cn/view/p/28819052019?f ...

  5. 20165309 2017-2018-2《Java程序设计》课程总结

    20165309 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:技能学习经验与C语言 预备作业3:Linux安装及学习 ...

  6. 『TensorFlow』变量初始化

    变量初始化实质 initializer操作的流程是调用Variable节点组中的Assign节点为节点操作单元分配初始值 变量初始化方法 tf.Variable_initializer([variab ...

  7. Lua 语言基本语法

    第一个 Lua 程序 .交互式编程 Lua 提供了交互式编程模式.我们可以在命令行中输入程序并立即查看效果. Lua 交互式编程模式可以通过命令 lua -i 或 lua 来启用 .脚本式编程 我们可 ...

  8. javascript中NAN undefined 和null

    null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性. NaN 属性是代表非数字值的特殊值.该属性用于指示某个值不是数字.可以把 Num ...

  9. c函数 文件名通配符

    static bool IsMatched(CONST TCHAR* p, CONST TCHAR* q) { CONST TCHAR *r, *z, *x = _T(""); f ...

  10. python全局变量

    定义函数里面的叫局部变量,出了函数外面就不能用了 局部变量函数被调用时,他的变量才生效 局部变量定义在内存里面,用完就会被释放,全局变量不会释放 当有相同名的局部变量和全局变量,函数会先找自己的变量, ...