android怎么对dialog截屏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android怎么对dialog截屏相关的知识,希望对你有一定的参考价值。

安卓系统手机操作系统相似,以华为手机为例,以下截屏方法您可以试一下:
一、屏幕下拉出现“开关”,点击“截屏”即可截图成功(如默认快捷开关没有截屏选项,需要点击编辑添加后方能使用)。
二、“截图”快捷键:同时按住“音量减”和“电源键”也可以实现截图。
三、通过指关节截屏:
1、截取完整屏幕:亮屏状态下连续敲击屏幕两下待弹窗自动退出即可完成截屏。
2、截取部分屏幕:亮屏状态下连续敲击屏幕两下,弹窗出现三秒内点击编辑,拖拽图片边框即可完成截屏。
3、画字母S滚动截屏:先用指关节敲击一下屏幕,然后不要离开屏幕,直接用指关节画字母S,点击滚动区域后完成截屏。
截屏成功后,图片文件默认保存在手机存储的Pictures/Screenshots文件夹下,也可以通过手机自带的图库查看。
参考技术A 解决方法 1:
请尝试使位图的活动窗口和对话框窗口,然后使用一个画布在海誓山盟层图像的位图。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
try
Bitmap bmOverlay = Bitmap.createBitmap(width, height, bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;

catch (Exception e) e.printStackTrace()

android实现截屏操作

最近开发了一些Android小游戏,想在游戏结束或者完成之后把整个屏幕截取下来并分享到社交平台上。先上效果吧。



网上一搜,截屏的方法很多。这里只贴出了一种,将截取到Bitmap赋给Dialog上的ImageView并弹出对话框。对对话框加了弹出和收起的动画。看起来就有截屏的感觉了。下面是所有代码。


弹出Dialog方法:包括截屏和弹出Dialog


/**
     * 游戏切图dialog分享
     */
    private void popShotSrceenDialog()
        final AlertDialog cutDialog = new AlertDialog.Builder(this).create();
        View dialogView = View.inflate(this, R.layout.show_cut_screen_layout, null);
        ImageView showImg = (ImageView) dialogView.findViewById(R.id.show_cut_screen_img);
        dialogView.findViewById(R.id.share_cancel).setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                cutDialog.dismiss();
            
        );
        dialogView.findViewById(R.id.share_img).setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Toast.makeText(ShotSrceenActivity.this,"点击了share按钮",Toast.LENGTH_SHORT).show();
            
        );
        //获取当前屏幕的大小
        int width = getWindow().getDecorView().getRootView().getWidth();
        int height = getWindow().getDecorView().getRootView().getHeight();
        //生成相同大小的图片
        Bitmap temBitmap = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
        //找到当前页面的跟布局
        View view = getWindow().getDecorView().getRootView();
        //设置缓存
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        //从缓存中获取当前屏幕的图片
        temBitmap = view.getDrawingCache();
        showImg.setImageBitmap(temBitmap);

        cutDialog.setView(dialogView);
        Window window = cutDialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (d.getHeight() * 0.8); // 高度设置为屏幕的0.6
        p.gravity = Gravity.CENTER;//设置弹出框位置
        window.setAttributes(p);
        window.setWindowAnimations(R.style.dialogWindowAnim);
        cutDialog.show();
    

Dialog布局就是图片加按钮:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/btn_layout"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/share_img"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="分享"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/share_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/show_cut_screen_img"
        android:layout_above="@id/btn_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

最后有个动画style和弹出动画以及收起动画:


<!--弹出对话框动画-->
    <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1">
        <item name="android:windowEnterAnimation">@anim/popview_in_amin</item>
        <item name="android:windowExitAnimation">@anim/popview_out_amin</item>
    </style>

弹出动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="200"/>
</set>

收起动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="0%"
        android:pivotY="100%"
        android:fillAfter="false"
        android:duration="200"/>
</set>

这样就完成了上图的效果,然后加入分享即可,网上说的截取屏幕用上面的方法所得到的图片状态栏位置是白色的一片。测试的时候我认为应该是透明的,解决方式是将状态栏设置为透明,然后再主布局中的最上面加上一个view来改变状态栏颜色,这样截取的Bitmap最上方状态栏就不会是白色的了。亲测是有效果的。


以上是关于android怎么对dialog截屏的主要内容,如果未能解决你的问题,请参考以下文章

android实现截屏操作

android中怎么用代码实现截屏

android 视频截图

Android 截屏方式

Android 4.0 ,如何截屏,当竖屏时。

android 怎么获取当前屏幕的截图