鸿蒙初学 实现图片的旋转与透明度的改变

参考资源: Android 图片旋转实现的两种方法的比较

布局文件 如下:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:id="$+id:dl"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">

    <Slider
        ohos:id="$+id:slider"
        ohos:height="20vp"
        ohos:width="match_parent"
        ohos:progress_hint_text="透明度"
        ohos:progress_hint_text_color="black"
        ohos:progress="100"
        ohos:top_margin="20vp"/>

    <Slider
        ohos:id="$+id:angle_slider"
        ohos:height="20vp"
        ohos:width="match_parent"
        ohos:progress_hint_text_color="black"
        ohos:progress_hint_text="旋转"
        ohos:top_margin="20vp"/>

    <Image
        ohos:id="$+id:image"
        ohos:height="match_content"
        ohos:width="300fp"
        ohos:image_src="$media:ycy"
        ohos:scale_mode="zoom_center"/>

</DirectionalLayout>

AbilitySlice 如下:

public class MainAbilitySlice extends AbilitySlice {
    
    
    static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG"); //MY_MODULE=0x00201
    Slider mSlider;
    Image mImage;
    private Slider mAngleSlider;
    private InputStream mInputStream;
    @Override
    public void onStart(Intent intent) {
    
    
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mSlider = (Slider) findComponentById(ResourceTable.Id_slider);
        mImage = (Image) findComponentById(ResourceTable.Id_image);
        mAngleSlider = (Slider) findComponentById(ResourceTable.Id_angle_slider);
        mSlider.setValueChangedListener(new Slider.ValueChangedListener() {
    
    
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
    
    
                HiLog.error(LABEL_LOG, "%{public}d", i);
                mImage.setAlpha(i / 100.0f);
            }

            @Override
            public void onTouchStart(Slider slider) {
    
    

            }

            @Override
            public void onTouchEnd(Slider slider) {
    
    

            }
        });
        mAngleSlider.setValueChangedListener(new Slider.ValueChangedListener() {
    
    
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
    
    
                InputStream inputStream = null;
                try {
    
    
                    inputStream = getResourceManager().getResource(ResourceTable.Media_ycy);
                    ImageSource.SourceOptions options = new ImageSource.SourceOptions();
                    options.formatHint = "image/jpg";
                    ImageSource imageSource = ImageSource.create(inputStream, options);
                    ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                    // 旋转
                    decodingOptions.rotateDegrees = 3.6f * i;
                    PixelMap pixelmap = imageSource.createPixelmap(decodingOptions);
                    mImage.setPixelMap(pixelmap);
                } catch (IOException | NotExistException e) {
    
    
                    HiLog.error(LABEL_LOG, "%{public}s", e.getMessage());
                }
            }

            @Override
            public void onTouchStart(Slider slider) {
    
    

            }

            @Override
            public void onTouchEnd(Slider slider) {
    
    

            }
        });
    }

    @Override
    public void onActive() {
    
    
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
    
    
        super.onForeground(intent);
    }
}

效果图:
实现效果图
**视频效果: **
鸿蒙初学—图片旋转与改变透明度。旋转很卡,应该是代码写的有问题。。。

做旋转的时候有明显的卡顿. 待解决

猜你喜欢

转载自blog.csdn.net/qq_41359651/article/details/119281274