最近在学习WP8,想实现WP微信中查看图片时的放大缩小功能。

网上找了两个解决方案:

1 利用GestureListener

这个类在Microsoft.Phone.Controls.Toolkit中,GestureListener可以捕捉到WP手机屏幕上的手势动作。

XAML文件:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image x:Name="_image" Source="/Assets/test.jpg" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<ScaleTransform x:Name="transform"/>
</Image.RenderTransform>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
DoubleTap="OnDoubleTap"DragDelta="OnDragDelta"
Flick="OnFlick"
PinchStarted="OnPinchStarted" PinchDelta="OnPinchDelta" PinchCompleted="OnPinchCompleted"/>
</toolkit:GestureService.GestureListener> </Image>
</Grid>

cs文件:

       double initialScale;

        public Zoom()
{
InitializeComponent();
} private void OnDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
transform.ScaleX = transform.ScaleY = ;
} private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
{
transform.CenterX -= e.HorizontalChange;
transform.CenterY -= e.VerticalChange;
} private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
{
initialScale = transform.ScaleX;
} private void OnPinchDelta(object sender, PinchGestureEventArgs e)
{
transform.ScaleX = transform.ScaleY = initialScale * e.DistanceRatio;
} private void OnPinchCompleted(object sender, PinchGestureEventArgs e)
{ } private void OnFlick(object sender, FlickGestureEventArgs e)
{ }

前面是在XAML代码中构建GestureListener,在后台代码中也可以构建GestureListener。

               Grid grd = new Grid
{
Height = ,
Width = ,
Background = new SolidColorBrush(Colors.Black),
Opacity = 0.9,
}; Image img = new Image { Source = source };
grd.Children.Add(img);
//设置图片变换类型为缩放
ScaleTransform transform = new ScaleTransform();
img.RenderTransform = transform; var gesListener= GestureService.GetGestureListener(img); gesListener.DoubleTap += (obj, arg) =>
{
transform.ScaleX = transform.ScaleY = ;
};
gesListener.DragDelta += (obj, arg) =>
{
transform.CenterX -= arg.HorizontalChange;
transform.CenterY -= arg.VerticalChange;
};
gesListener.PinchStarted += (obj, arg) =>
{
initialScale = transform.ScaleX;
};
gesListener.PinchDelta += (obj, arg) =>
{
transform.ScaleX = transform.ScaleY = initialScale * arg.DistanceRatio;
};

2 ViewportControl

XAML文件:

       <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ViewportControl x:Name="viewport"
ManipulationStarted="OnManipulationStarted" ManipulationDelta="OnManipulationDelta"
ManipulationCompleted="OnManipulationCompleted" ViewportChanged="viewport_ViewportChanged">
<Canvas x:Name="canvas">
<Image x:Name="TestImage" Source="/Assets/test.jpg"
RenderTransformOrigin="0,0" CacheMode="BitmapCache"
ImageOpened="OnImageOpened">
<Image.RenderTransform>
<ScaleTransform x:Name="xform"/>
</Image.RenderTransform>
</Image>
</Canvas>
</ViewportControl>
</Grid>

cs文件:

 const double MaxScale = ;

        double _scale = 1.0;
double _minScale;
double _coercedScale;
double _originalScale; Size _viewportSize;
bool _pinching;
Point _screenMidpoint;
Point _relativeMidpoint; BitmapImage _bitmap; public PinchAndZoom()
{
InitializeComponent(); BuildLocalizedApplicationBar();
} /// <summary>
/// Either the user has manipulated the image or the size of the viewport has changed. We only
/// care about the size.
/// </summary>
void viewport_ViewportChanged(object sender, System.Windows.Controls.Primitives.ViewportChangedEventArgs e)
{
Size newSize = new Size(viewport.Viewport.Width, viewport.Viewport.Height);
if (newSize != _viewportSize)
{
_viewportSize = newSize;
CoerceScale(true);
ResizeImage(false);
}
} /// <summary>
/// Handler for the ManipulationStarted event. Set initial state in case
/// it becomes a pinch later.
/// </summary>
void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
_pinching = false;
_originalScale = _scale;
} /// <summary>
/// Handler for the ManipulationDelta event. It may or may not be a pinch. If it is not a
/// pinch, the ViewportControl will take care of it.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
if (e.PinchManipulation != null)
{
e.Handled = true; if (!_pinching)
{
_pinching = true;
Point center = e.PinchManipulation.Original.Center;
_relativeMidpoint = new Point(center.X / TestImage.ActualWidth, center.Y / TestImage.ActualHeight); var xform = TestImage.TransformToVisual(viewport);
_screenMidpoint = xform.Transform(center);
} _scale = _originalScale * e.PinchManipulation.CumulativeScale; CoerceScale(false);
ResizeImage(false);
}
else if (_pinching)
{
_pinching = false;
_originalScale = _scale = _coercedScale;
}
} /// <summary>
/// The manipulation has completed (no touch points anymore) so reset state.
/// </summary>
void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
_pinching = false;
_scale = _coercedScale;
} /// <summary>
/// When a new image is opened, set its initial scale.
/// </summary>
void OnImageOpened(object sender, RoutedEventArgs e)
{
_bitmap = (BitmapImage)TestImage.Source; // Set scale to the minimum, and then save it.
_scale = ;
CoerceScale(true);
_scale = _coercedScale; ResizeImage(true);
} /// <summary>
/// Adjust the size of the image according to the coerced scale factor. Optionally
/// center the image, otherwise, try to keep the original midpoint of the pinch
/// in the same spot on the screen regardless of the scale.
/// </summary>
/// <param name="center"></param>
void ResizeImage(bool center)
{
if (_coercedScale != && _bitmap != null)
{
double newWidth = canvas.Width = Math.Round(_bitmap.PixelWidth * _coercedScale);
double newHeight = canvas.Height = Math.Round(_bitmap.PixelHeight * _coercedScale); xform.ScaleX = xform.ScaleY = _coercedScale; viewport.Bounds = new Rect(, , newWidth, newHeight); if (center)
{
viewport.SetViewportOrigin(
new Point(
Math.Round((newWidth - viewport.ActualWidth) / ),
Math.Round((newHeight - viewport.ActualHeight) / )
));
}
else
{
Point newImgMid = new Point(newWidth * _relativeMidpoint.X, newHeight * _relativeMidpoint.Y);
Point origin = new Point(newImgMid.X - _screenMidpoint.X, newImgMid.Y - _screenMidpoint.Y);
viewport.SetViewportOrigin(origin);
}
}
} /// <summary>
/// Coerce the scale into being within the proper range. Optionally compute the constraints
/// on the scale so that it will always fill the entire screen and will never get too big
/// to be contained in a hardware surface.
/// </summary>
/// <param name="recompute">Will recompute the min max scale if true.</param>
void CoerceScale(bool recompute)
{
if (recompute && _bitmap != null && viewport != null)
{
// Calculate the minimum scale to fit the viewport
double minX = viewport.ActualWidth / _bitmap.PixelWidth;
double minY = viewport.ActualHeight / _bitmap.PixelHeight; _minScale = Math.Min(minX, minY);
} _coercedScale = Math.Min(MaxScale, Math.Max(_scale, _minScale)); } // Sample code for building a localized ApplicationBar
private void BuildLocalizedApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar(); // Create a new button and set the text value to the localized string from AppResources.
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/appbar_info.png", UriKind.Relative));
appBarButton.Click += appBarButton_Click;
appBarButton.Text = AppResources.AppBarButtonInfoText;
ApplicationBar.Buttons.Add(appBarButton); } void appBarButton_Click(object sender, EventArgs e)
{
MessageBox.Show(AppResources.PinchZoomHelpText, AppResources.InfoCaption,MessageBoxButton.OK);
}

PS:测试的话应该部署到WP8手机中测试。

参考:

http://www.cnblogs.com/chengxingliang/archive/2011/08/15/2137377.html

http://www.cnblogs.com/JerryH/archive/2012/01/05/2312635.html

http://code.msdn.microsoft.com/wpapps/Image-Recipes-0c0b8fee

WP8图片缩放功能实现的更多相关文章

  1. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

  2. UIScrollView控件实现图片缩放功能

    转发自:http://www.cnblogs.com/wendingding/p/3754268.html 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScr ...

  3. iOS UI-UIScrollView控件实现图片缩放功能

    一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理.也就是说,要完成缩放功能的话,只需要将需要缩 ...

  4. HTML5 图片缩放功能

    腾讯新闻上用的插件(xw.qq.com) 缩放插件scale.js (function(window, undefined) { var document = window.document, sup ...

  5. php实现图片缩放功能类

    http://www.poluoluo.com/jzxy/201312/255447.html <?php /** * Images类是一个图片处理类 * @package applicatio ...

  6. Python之图片缩放功能实现

    这几天由于有项目在做,自己的学习部分然后没有很充足的时间,但是这些零碎的时间也是很宝贵的,所以还是继续学我的python,我很喜欢这个语言,因为简洁,开发环境简单,更多的事,功能灰常的强大,所以好多有 ...

  7. iOS开发基础-UIScrollView实现图片缩放

    当用户在 UIScrollView 上使用捏合手势时, UIScrollView 会给 UIScrollViewDelegate 协议发送一条消息,并调用代理的 viewForZoomingInScr ...

  8. 使用Martix来实现缩放图片的功能

    使用Martix(android.graphics.Matrix)类中的postScale()方法结合Bitmap来实现缩放图片的功能 Bitmap bmp = BitmapFactory.decod ...

  9. C# 图片缩放,拖拽后保存成图片的功能

    窗体界面部分如下: 鼠标的缩放功能需要手动在 OpertaionImg.Designer.cs 文件里面添加一句代码,具体代码如下: //picturePhoto显示图片的控件 this.pictur ...

随机推荐

  1. django 初级(一) 配置与周边

    一.下载安装 从 https://www.djangoproject.com/download/ 下载最新的 django 版本,写本文时最新版本为 django 1.7,官方说只需要 python6 ...

  2. webapp中fixed问题解决方案

    主要问题: 1,头部输入框固定后,只要再滑动内容的话,输入框会随着滑动内容而滑动. 2,在低端机:2.3以下的安卓机,你会发现怎么解决都不行的,系统浏览器是不会支持的,头部底部固定的话会滑动内容而滑动 ...

  3. ggplo2学习笔记——基本图形类型

    1.散点图:又称散点分布图,是以一个变量为恨坐标,另一个变量为纵坐标,利用散点(坐标点)的分布形态反映变量统计关系的一种图形.可以用来确认两个变量之间的关系.绘制自由曲线.矩阵关联分析等. 2.条形图 ...

  4. ytkah网站建设解决方案 大中小微企业营销利器

    为大中小微企业提供网站设计制作优化服务,PC移动微网站三合一,抢占市场先机.读万卷书不如走万里路,走万里路不如阅人无数.说再多空洞无物不如上案例几簇 优秀案例展示,上市公司人人网旗下游戏<天书奇 ...

  5. join 和 union 区别

    JOIN和UNION区别 join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 .JOIN用于按照ON条件联接两个表 ...

  6. 导航栏的坑 (导航透明/导航除线/titleIView)

    //以下四个条件缺一不可 /.必须是半透明状态 self.navigationBar.translucent = YES; //2.导航栏背景图片为空图片 (不可以设置backgroundColor或 ...

  7. word20161204

    CA, certification authority / 证书颁发机构 cache / 高速缓存 cache file / 缓存文件 caching / 缓存 caching resolver /  ...

  8. IntelliJ中的main函数和System.out.println()快捷键

    1.在IntelJ中和Eclipse中稍有不同,在Eclipse中,输入main再按Alt+/即可自动补全main函数,但是在IntellJ中则是输入psvm,选中即可 2.在方法体内部有for循环, ...

  9. BZOJ 2822: [AHOI2012]树屋阶梯

    Description 求拼成阶梯状的方案数. Sol 高精度+Catalan数. 我们可以把最后一行无线延伸,所有就很容易看出Catalan数了. \(f_n=f_0f_{n-1}+f_1f_{n- ...

  10. CodeVS 2845 排序的代价

    Description 给你一个数列使他递增,交换两个元素的代价为两个数的和,最小化代价. Sol 置换群+离散化. 使一个数列恢复递增顺序,那么,他和他要到达的位置的数需要交换,这样就形成了一个置换 ...