Android图片处理--全景查看效果

时间:2023-03-09 05:34:39
Android图片处理--全景查看效果

PS:Android对于图片处理这块资源还是挺多的,之前用OpenGL制作图片的全景效果,耗时耗力,而且只能点击进去后看到,但是效果是非常的号,今天所写的是编写好的一个图片控件,只要拿来用就可以了。效果不是那么好,处理的之后就是一张图片截取中间部分放大再显示在屏幕中间,通过摆动手机查看被遮挡部分,如图:一开始图片是这样的

Android图片处理--全景查看效果Android图片处理--全景查看效果

上面就是效果图了

1:添加依赖

//全景图片
compile 'com.gjiazhe:PanoramaImageView:1.0'

2:使用控件

<com.gjiazhe.panoramaimageview.PanoramaImageView
android:id="@+id/panorama_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/timg"
app:piv_enablePanoramaMode="true"
app:piv_show_scrollbar="true"
app:piv_invertScrollDirection="false" />

布局的根目录一定要加上

    xmlns:app="http://schemas.android.com/apk/res-auto"

这里面有三个属性(其中三个)

一个是app:piv_enablePanoramaMode,使用全景效果模式,app:piv_show_scrollbar滚动条显示,app:piv_invertScrollDirection颠倒滚动方向,不同的值就会呈现不同的效果。

3:注册GyroscopeObserver

在使用PanoramaImageView的Activity或Fragment中,您应该在onResume()中注册GyroscopeObserver,并记得在onPause()中注销它。

public class MyActivity extends AppCompatActivity {

    private GyroscopeObserver gyroscopeObserver;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize GyroscopeObserver.
gyroscopeObserver = new GyroscopeObserver();
// Set the maximum radian the device should rotate to show image's bounds.
// It should be set between 0 and π/2.
// The default value is π/9.
gyroscopeObserver.setMaxRotateRadian(Math.PI/); PanoramaImageView panoramaImageView = (PanoramaImageView) findViewById(R.id.panorama_image_view);
// Set GyroscopeObserver for PanoramaImageView.
panoramaImageView.setGyroscopeObserver(gyroscopeObserver);
} @Override
protected void onResume() {
super.onResume();
// Register GyroscopeObserver.
gyroscopeObserver.register(this);
} @Override
protected void onPause() {
super.onPause();
// Unregister GyroscopeObserver.
gyroscopeObserver.unregister();
}
}

设置OnPanoramaScrollListener以观察滚动状态 如果要在图像滚动时获得回调,PanoramaImageView需要设置OnPanoramaScrollListener。

panoramaImageView.setOnPanoramaScrollListener(new PanoramaImageView.OnPanoramaScrollListener() {
@Override
public void onScrolled(PanoramaImageView view, float offsetProgress) {
// Do something here.
// The offsetProgress range from -1 to 1, indicating the image scrolls
// from left(top) to right(bottom).
}
});