app直播源码,图片根据手指位置移动

app直播源码,图片根据手指位置移动实现的相关代码

页面布局

用于存放图片的容器(帧布局)

 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/FrameLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
</FrameLayout>

 

继承自View的自定义组件类

 

LogoView.cs
using Android.Content;
using Android.Graphics;
using Android.Views;
namespace android_by_csharp
{
    public class LogoView : View
    {
        public float BitmapX;
        public float BitmapY;
        public LogoView(Context context) : base(context)
        {
            // 初始化图形要出现的坐标位置
            BitmapX = 0;
            BitmapY = 0;
        }
        // 重写OnDraw方法
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas); 
            // 画笔
            var paint = new Paint();
            // 生成位图对象
            var bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.mini_logo);
            if (bitmap == null) return;
            // 绘制位图
            canvas.DrawBitmap(bitmap, BitmapX - (float)bitmap.Width / 2, BitmapY - (float)bitmap.Height / 2, paint);
            // 判断图片是否回收,木有回收的话强制收回图片  
            if (!bitmap.IsRecycled)
                bitmap.Recycle();
        }
    }
}

 

 

在主类中引入自定义的组件类并想布局中添加图片

 

using Android.App;
using Android.OS;
using Android.Widget;
namespace android_by_csharp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            // 获取帧布局
            var frame = (FrameLayout)FindViewById(Resource.Id.FrameLayout1);
            var logo = new LogoView(this);
            logo.Touch += (s, e) =>
            {
                if (e.Event == null) return;
                logo.BitmapX = e.Event.GetX();
                logo.BitmapY = e.Event.GetY();
                // 调用重绘方法
                logo.Invalidate();
            };
            // 添加logo图标
            frame?.AddView(logo);
        }
    }
}

 

以上就是app直播源码,图片根据手指位置移动实现的相关代码, 更多内容欢迎关注之后的文章

 

posted @ 2021-10-22 14:17  云豹科技-苏凌霄  阅读(69)  评论(0编辑  收藏  举报