Android行动相簿:使用GridView显示照片缩略图

需积分: 12 1 下载量 90 浏览量 更新于2024-07-21 收藏 1.51MB PPT 举报
"该资源是一个关于Android应用开发的教程,主要讲解如何创建一个显示照片缩略图的GridView相册,并在选择缩略图后通过ImageView展示原图。内容包括使用GridView和ImageView组件来实现移动设备上的照片浏览功能,以及相关的项目结构和步骤解释。" 在Android开发中,创建一个类似相册的应用程序是常见的需求。本教程通过一个名为“bq76PL536A-Q1”的示例,详细介绍了如何构建一个基本的图片浏览应用。这个应用由两个主要部分组成:一个是显示照片缩略图的GridView,另一个是用于全屏查看选中照片的ImageView。 1. GridView组件:GridView是一个二维布局控件,能够以网格形式显示列表项目。在这个例子中,它被用来展示照片的缩略图目录。通过设置`android:stretchMode="columnWidth"`,我们可以确保每个列的宽度一致,`android:columnWidth="90dp"`定义了每个单元格的宽度,`android:numColumns="auto_fit"`允许根据屏幕宽度自动调整列数。 2. 图片资源:在Android应用中,图片通常存储在`res/drawable`目录下。在这个案例中,所有照片都以PNG格式存放,作为应用的图形资源。开发者需要确保这些图片文件已添加到项目中。 3. MainActivity类:这是应用的主要活动,负责显示照片缩略图的GridView。在布局XML文件中,需要删除不需要的TextView元素并添加GridView。在代码层面,MainActivity需要设置适配器(如ArrayAdapter或CursorAdapter)来填充GridView,将图片资源与每个单元格关联。 4. 选择事件处理:当用户点击GridView中的某个缩略图时,应启动一个新的活动(例如,FullImageActivity)并在其中显示选定的照片。这通常通过Intent来实现,Intent携带选定图片的标识或路径,新活动接收Intent并使用ImageView加载图片。 5. ImageView组件:在FullImageActivity中,ImageView用于显示全尺寸的照片。它可以填充整个屏幕,提供更好的用户体验。可以使用`setImageResource()`或`setImageBitmap()`方法设置图片。 6. 应用流程:首先,用户打开应用看到GridView显示的缩略图;接着,用户点击一个缩略图,系统启动新的活动并传递相关信息;最后,FullImageActivity的ImageView加载并显示所选照片。 通过这个教程,开发者可以学习到Android中如何管理和展示图片,这对于创建多媒体应用程序,尤其是照片分享或相册类应用至关重要。同时,这也涉及到用户界面设计和活动间的交互,这些都是Android开发的基础技能。

ValueError Traceback (most recent call last) <ipython-input-54-536a68c200e5> in <module> 52 return model 53 # lstm network ---> 54 model = create_LSTM_model(X_train,n_steps,n_length, n_features) 55 # summary 56 print(model.summary()) <ipython-input-54-536a68c200e5> in create_LSTM_model(X_train, n_steps, n_length, n_features) 22 X_train = X_train.reshape((X_train.shape[0], n_steps, 1, n_length, n_features)) 23 ---> 24 model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', 25 input_shape=(n_steps, 1, n_length, n_features))) 26 model.add(Flatten()) ~\anaconda3\lib\site-packages\tensorflow\python\trackable\base.py in _method_wrapper(self, *args, **kwargs) 203 self._self_setattr_tracking = False # pylint: disable=protected-access 204 try: --> 205 result = method(self, *args, **kwargs) 206 finally: 207 self._self_setattr_tracking = previous_value # pylint: disable=protected-access ~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs) 68 # To get the full stack trace, call: 69 # `tf.debugging.disable_traceback_filtering()` ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb ~\anaconda3\lib\site-packages\keras\engine\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name) 233 ndim = shape.rank 234 if ndim != spec.ndim: --> 235 raise ValueError( 236 f'Input {input_index} of layer "{layer_name}" ' 237 "is incompatible with the layer: " ValueError: Input 0 of layer "conv_lstm2d_12" is incompatible with the layer: expected ndim=5, found ndim=3. Full shape received: (None, 10, 5)解决该错误

2023-05-26 上传

def create_LSTM_model(X_train,n_steps,n_length, n_features): # instantiate the model model = Sequential() model.add(Input(shape=(X_train.shape[1], X_train.shape[2]))) X_train = X_train.reshape((X_train.shape[0], n_steps, 1, n_length, n_features)) model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_steps, 1, n_length, n_features))) model.add(Flatten()) # cnn1d Layers # 添加lstm层 model.add(LSTM(64, activation = 'relu', return_sequences=True)) model.add(Dropout(0.5)) #添加注意力层 model.add(LSTM(64, activation = 'relu', return_sequences=False)) # 添加dropout model.add(Dropout(0.5)) model.add(Dense(128)) # 输出层 model.add(Dense(1, name='Output')) # 编译模型 model.compile(optimizer='adam', loss='mse', metrics=['mae']) return model # lstm network model = create_LSTM_model(X_train,n_steps,n_length, n_features) # summary print(model.summary())修改该代码,解决ValueError Traceback (most recent call last) <ipython-input-54-536a68c200e5> in <module> 52 return model 53 # lstm network ---> 54 model = create_LSTM_model(X_train,n_steps,n_length, n_features) 55 # summary 56 print(model.summary()) <ipython-input-54-536a68c200e5> in create_LSTM_model(X_train, n_steps, n_length, n_features) 22 X_train = X_train.reshape((X_train.shape[0], n_steps, 1, n_length, n_features)) 23 ---> 24 model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', 25 input_shape=(n_steps, 1, n_length, n_features))) 26 model.add(Flatten()) ~\anaconda3\lib\site-packages\tensorflow\python\trackable\base.py in _method_wrapper(self, *args, **kwargs) 203 self._self_setattr_tracking = False # pylint: disable=protected-access 204 try: --> 205 result = method(self, *args, **kwargs) 206 finally: 207 self._self_setattr_tracking = previous_value # pylint: disable=protected-access ~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs) 68 # To get the full stack trace, call: 69 # tf.debugging.disable_traceback_filtering() ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb ~\anaconda3\lib\site-packages\keras\engine\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name) 233 ndim = shape.rank 234 if ndim != spec.ndim: --> 235 raise ValueError( 236 f'Input {input_index} of layer "{layer_name}" ' 237 "is incompatible with the layer: " ValueError: Input 0 of layer "conv_lstm2d_12" is incompatible with the layer: expected ndim=5, found ndim=3. Full shape received: (None, 10, 5)错误

2023-05-26 上传